Contenu connexe Similaire à Go初心者がGoでコマンドラインツールの作成に挑戦した話 (20) Go初心者がGoでコマンドラインツールの作成に挑戦した話28. ## ( )
$ mkdir test && cd test
$ go get github.com/spf13/cobra/cobra
##
$ cat 'author: Yuki Haneda' > ~/.cobra.yaml
$ cat 'license: MIT' >> ~/.cobra.yaml
# viper( )
$ cobra init --viper=false
31. // hogeCmd represents the hoge command
var hogeCmd = &cobra.Command{
Use: "hoge",
Short: " ",
Long: ` `,
Run: func(cmd *cobra.Command, args []string) {
//
},
}
34. // hogeCmd represents the hoge command
var hogeCmd = &cobra.Command{
Use: "hoge",
Short: " ",
Long: ` `,
Run: func(cmd *cobra.Command, args []string) {
//
},
}
39. var Conf Config
type Config struct {
Core CoreConfig
Jira JiraConfig
}
type CoreConfig struct {
Editor string `toml:"editor"`
TicketCache string `toml:"ticketcache"`
SelectCmd string `toml:"selectcmd"`
}
type JiraConfig struct {
Host string `toml:"host"`
UserName string `toml:"username"`
Password string `toml:"password"`
Jql string `toml:"jql"`
}
40. file := "/tmp"
f, err := os.Create(file)
if err != nil {
return err
}
toml.NewEncoder(f).Encode(conf)
41. var tickets Tickets
type Tickets struct {
Tickets []Ticket `toml:"Tickets"`
}
type Ticket struct {
ID string `toml:"id"`
Title string `toml:"title"`
Description string `toml:"description"`
}
42. file := "/tmp"
if _, err := toml.DecodeFile(cache, tickets);
err != nil {
return err
}
46. //
func run(command string, r io.Reader, w io.Writer) error {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", command)
} else {
cmd = exec.Command("sh", "-c", command)
}
cmd.Stderr = os.Stderr
cmd.Stdout = w
cmd.Stdin = r
return cmd.Run()
}
53. // list fzf
list := `first line
second line
third line`
var buf bytes.Buffer //
if err := run("fzf", strings.NewReader(list), &buf);
err != nil {
return nil
}