Skip to content

Commit

Permalink
feat: add web
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Oct 4, 2018
1 parent 1d773ec commit 93c1f37
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
3 changes: 0 additions & 3 deletions cmd_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
)

type dbOptions struct {
Path string `mapstructure:"dbpath"`
Verbose bool
}

func (opts dbOptions) String() string {
Expand All @@ -22,7 +20,6 @@ func (opts dbOptions) String() string {
}

func dbSetupFlags(flags *pflag.FlagSet, opts *dbOptions) {

viper.BindPFlags(flags)
}

Expand Down
52 changes: 52 additions & 0 deletions cmd_web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

type webOptions struct {
// web specific
Bind string

// db
DBOpts dbOptions
}

func (opts webOptions) String() string {
out, _ := json.Marshal(opts)
return string(out)
}

func webSetupFlags(flags *pflag.FlagSet, opts *webOptions) {
flags.StringVarP(&opts.Bind, "bind", "b", ":2020", "web server bind address")
viper.BindPFlags(flags)
}

func newWebCommand() *cobra.Command {
opts := &webOptions{}
cmd := &cobra.Command{
Use: "web",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
if err := viper.Unmarshal(&opts.DBOpts); err != nil {
return err
}
return web(opts)
},
}
webSetupFlags(cmd.Flags(), opts)
dbSetupFlags(cmd.Flags(), &opts.DBOpts)
return cmd
}

func web(opts *webOptions) error {
fmt.Println("web", opts)
return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func newRootCommand() *cobra.Command {
newPullCommand(),
newRunCommand(),
newDBCommand(),
newWebCommand(),
)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand Down

0 comments on commit 93c1f37

Please sign in to comment.