diff --git a/application/config.go b/application/config.go index 3197a039..4ada3927 100644 --- a/application/config.go +++ b/application/config.go @@ -91,6 +91,11 @@ func (c *Configuration) Set(path string) error { return yaml.NewDecoder(bytes.NewReader(data)).Decode(c) } +// Type returns value type of itself +func (c *Configuration) Type() string { + return "string" +} + // Addr returns a string of server port func (c *Configuration) Addr() string { return fmt.Sprintf(":%d", c.Server.Port) diff --git a/application/config_test.go b/application/config_test.go index 37abc687..81e37451 100644 --- a/application/config_test.go +++ b/application/config_test.go @@ -109,6 +109,16 @@ func TestConfiguration_Set(t *testing.T) { }) } +func TestConfiguration_Type(t *testing.T) { + // when + actual := application.Config.Type() + + // expect + if actual != "string" { + t.Errorf("type should equal string, but got %+v", actual) + } +} + func TestConfiguration_Addr(t *testing.T) { // given application.Config.Server.Port = 8823 diff --git a/go.mod b/go.mod index e5df9e5e..7133b723 100644 --- a/go.mod +++ b/go.mod @@ -45,6 +45,8 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sergi/go-diff v1.0.0 // indirect github.com/sirupsen/logrus v1.0.6 // indirect + github.com/spf13/cobra v0.0.3 + github.com/spf13/pflag v1.0.3 // indirect github.com/src-d/gcfg v1.3.0 // indirect github.com/stretchr/testify v1.2.2 // indirect github.com/syndtr/goleveldb v0.0.0-20180815032940-ae2bd5eed72d diff --git a/main.go b/main.go index ba685b02..cd69d216 100644 --- a/main.go +++ b/main.go @@ -1,21 +1,34 @@ package main import ( - "flag" "github.com/duck8823/duci/application" "github.com/duck8823/duci/application/semaphore" "github.com/duck8823/duci/infrastructure/logger" "github.com/duck8823/duci/presentation/router" "github.com/google/uuid" + "github.com/spf13/cobra" "net/http" "os" ) func main() { - mainID := uuid.New() + serverCmd := &cobra.Command{ + Use: "server", + Run: serverCmd, + } + serverCmd.PersistentFlags().VarP(application.Config, "config", "c", "configuration file path") + + rootCmd := &cobra.Command{Use: "duci"} + rootCmd.AddCommand(serverCmd) + + if err := rootCmd.Execute(); err != nil { + logger.Errorf(uuid.New(), "Failed to execute command.\n%+v", err) + os.Exit(1) + } +} - flag.Var(application.Config, "c", "configuration file path") - flag.Parse() +func serverCmd(cmd *cobra.Command, _ []string) { + mainID := uuid.New() if err := semaphore.Make(); err != nil { logger.Errorf(mainID, "Failed to initialize a semaphore.\n%+v", err)