Go Cobra 활용

w00j00ng351·2022년 11월 9일
0

개발도구

목록 보기
3/4

1. 내용

2. 설치

$ go install github.com/spf13/cobra-cli@latest

3. 활용 예

3.1. Go 모듈 초기화

$ go mod init goproj
go: creating new go.mod: module goproj

3.2. Go Cobra 초기화

$ cobra-cli init -a w00j00ng351@gmail.com
Your Cobra application is ready at
/go/src/goproj
  • 명령 수행 후 main.go, LICENSE, go.sum, cmd/root.go 파일이 생성되고, go.mod 파일에 종속성 정보가 추가됨

  • main.go 파일내용 확인

/*
Copyright © 2022 w00j00ng351@gmail.com

*/
package main

import "goproj/cmd"

func main() {
	cmd.Execute()
}
  • cmd/root.go 파일내용 확인
/*
Copyright © 2022 w00j00ng351@gmail.com

*/
package cmd

import (
	"os"

	"github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "goproj",
	Short: "A brief description of your application",
	Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	// Uncomment the following line if your bare application
	// has an action associated with it:
	// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

func init() {
	// Here you will define your flags and configuration settings.
	// Cobra supports persistent flags, which, if defined here,
	// will be global for your application.

	// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.goproj.yaml)")

	// Cobra also supports local flags, which will only run
	// when this action is called directly.
	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

3.2. Go Cobra 명령 추가

$ cobra-cli add serve -a w00j00ng351@gmail.com
serve created at /home/player/source/private/goproj
  • cmd/serve.go 파일 추가됨
/*
Copyright © 2022 w00j00ng351@gmail.com

*/
package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
	Use:   "serve",
	Short: "A brief description of your command",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("serve called")
	},
}

func init() {
	rootCmd.AddCommand(serveCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// serveCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

3.4. 프로그램 실행 및 결과 확인

  • 인자 없이 프로그램 호출
$ go run main.go
A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

Usage:
  goproj [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  serve       A brief description of your command

Flags:
  -h, --help     help for goproj
  -t, --toggle   Help message for toggle

Use "goproj [command] --help" for more information about a command.
  • serve 인자를 입력하고 프로그램 호출
$ go run main.go serve
serve called
profile
시간이 만든 코드

0개의 댓글