cli: access flags using viper rather than cobra

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-25 16:11:52 -07:00
parent f59b30a27c
commit 1e8cef9ad0
6 changed files with 9 additions and 24 deletions

View File

@ -8,22 +8,14 @@ import (
"dagger.io/go/dagger"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// getRouteName returns the selected route name (based on explicit CLI selection or current work dir)
func getRouteName(ctx context.Context, cmd *cobra.Command) string {
func getRouteName(ctx context.Context) string {
lg := log.Ctx(ctx)
routeName, err := cmd.Flags().GetString("route")
if err != nil {
lg.
Fatal().
Err(err).
Str("flag", "route").
Msg("unable to resolve flag")
}
routeName := viper.GetString("route")
if routeName != "" {
return routeName
}

View File

@ -31,7 +31,7 @@ var computeCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
name := getRouteName(ctx, cmd)
name := getRouteName(ctx)
st := &dagger.RouteState{
ID: uuid.New().String(),
Name: name,

View File

@ -25,7 +25,7 @@ var downCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(ctx, cmd)
routeName := getRouteName(ctx)
route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil {
lg.

View File

@ -24,16 +24,9 @@ var newCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
upRouteFlag, err := cmd.Flags().GetBool("up")
if err != nil {
lg.
Fatal().
Err(err).
Str("flag", "up").
Msg("unable to resolve flag")
}
upRouteFlag := viper.GetBool("up")
routeName := getRouteName(ctx, cmd)
routeName := getRouteName(ctx)
// TODO: Implement options: --layout-*, --setup
route, err := store.CreateRoute(ctx, routeName, nil)

View File

@ -26,7 +26,7 @@ var queryCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(ctx, cmd)
routeName := getRouteName(ctx)
route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil {
lg.

View File

@ -25,7 +25,7 @@ var upCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(ctx, cmd)
routeName := getRouteName(ctx)
route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil {
lg.