From 576613e46ad54e398a7316542c68bc673146111c Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 26 Mar 2021 15:50:18 -0700 Subject: [PATCH] implemented inputs Signed-off-by: Sam Alba --- cmd/dagger/cmd/{ => common}/common.go | 25 ++++++++++++------- cmd/dagger/cmd/compute.go | 3 ++- cmd/dagger/cmd/down.go | 3 ++- cmd/dagger/cmd/input/container.go | 12 ++++++---- cmd/dagger/cmd/input/dir.go | 12 ++++++---- cmd/dagger/cmd/input/git.go | 17 +++++++++---- cmd/dagger/cmd/input/root.go | 28 ++++++++++++++++++++-- cmd/dagger/cmd/input/secret.go | 4 ++-- cmd/dagger/cmd/input/{value.go => text.go} | 18 +++++++------- cmd/dagger/cmd/new.go | 3 ++- cmd/dagger/cmd/query.go | 3 ++- cmd/dagger/cmd/up.go | 5 ++-- 12 files changed, 91 insertions(+), 42 deletions(-) rename cmd/dagger/cmd/{ => common}/common.go (79%) rename cmd/dagger/cmd/input/{value.go => text.go} (52%) diff --git a/cmd/dagger/cmd/common.go b/cmd/dagger/cmd/common/common.go similarity index 79% rename from cmd/dagger/cmd/common.go rename to cmd/dagger/cmd/common/common.go index 0f479395..155d9133 100644 --- a/cmd/dagger/cmd/common.go +++ b/cmd/dagger/cmd/common/common.go @@ -1,4 +1,4 @@ -package cmd +package common import ( "context" @@ -11,7 +11,19 @@ import ( ) // getCurrentRoute returns the current selected route based on its abs path -func getCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route { +func GetCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route { + lg := log.Ctx(ctx) + st := GetCurrentRouteState(ctx, store) + + route, err := dagger.NewRoute(st) + if err != nil { + lg.Fatal().Err(err).Interface("routeState", st).Msg("failed to init route") + } + + return route +} + +func GetCurrentRouteState(ctx context.Context, store *dagger.Store) *dagger.RouteState { lg := log.Ctx(ctx) var ( @@ -37,15 +49,10 @@ func getCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route { } } - route, err := dagger.NewRoute(st) - if err != nil { - lg.Fatal().Err(err).Interface("routeState", st).Msg("failed to init route") - } - - return route + return st } -func routeUp(ctx context.Context, route *dagger.Route) { +func RouteUp(ctx context.Context, route *dagger.Route) { lg := log.Ctx(ctx) c, err := dagger.NewClient(ctx, "") diff --git a/cmd/dagger/cmd/compute.go b/cmd/dagger/cmd/compute.go index dc220c4b..86054007 100644 --- a/cmd/dagger/cmd/compute.go +++ b/cmd/dagger/cmd/compute.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "dagger.io/go/cmd/dagger/cmd/common" "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" "go.mozilla.org/sops" @@ -131,7 +132,7 @@ var computeCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("unable to initialize route") } - routeUp(ctx, route) + common.RouteUp(ctx, route) }, } diff --git a/cmd/dagger/cmd/down.go b/cmd/dagger/cmd/down.go index 447b4c2a..cce8255e 100644 --- a/cmd/dagger/cmd/down.go +++ b/cmd/dagger/cmd/down.go @@ -2,6 +2,7 @@ package cmd import ( + "dagger.io/go/cmd/dagger/cmd/common" "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" @@ -29,7 +30,7 @@ var downCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load store") } - route := getCurrentRoute(ctx, store) + route := common.GetCurrentRoute(ctx, store) // TODO: Implement options: --no-cache if err := route.Down(ctx, nil); err != nil { diff --git a/cmd/dagger/cmd/input/container.go b/cmd/dagger/cmd/input/container.go index 5187ca9b..1ec31366 100644 --- a/cmd/dagger/cmd/input/container.go +++ b/cmd/dagger/cmd/input/container.go @@ -1,14 +1,16 @@ package input import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" "github.com/spf13/cobra" "github.com/spf13/viper" ) var containerCmd = &cobra.Command{ - Use: "container ID", + Use: "container TARGET CONTAINER-IMAGE", Short: "Add a container image as input artifact", - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -17,10 +19,10 @@ var containerCmd = &cobra.Command{ } }, Run: func(cmd *cobra.Command, args []string) { - // lg := logger.New() - // ctx := lg.WithContext(cmd.Context()) + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) - panic("not implemented") + updateRouteInput(ctx, args[0], dagger.DockerInput(args[1])) }, } diff --git a/cmd/dagger/cmd/input/dir.go b/cmd/dagger/cmd/input/dir.go index 170aa955..809a1401 100644 --- a/cmd/dagger/cmd/input/dir.go +++ b/cmd/dagger/cmd/input/dir.go @@ -1,14 +1,16 @@ package input import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" "github.com/spf13/cobra" "github.com/spf13/viper" ) var dirCmd = &cobra.Command{ - Use: "dir PATH", + Use: "dir TARGET PATH", Short: "Add a local directory as input artifact", - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -17,10 +19,10 @@ var dirCmd = &cobra.Command{ } }, Run: func(cmd *cobra.Command, args []string) { - // lg := logger.New() - // ctx := lg.WithContext(cmd.Context()) + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) - panic("not implemented") + updateRouteInput(ctx, args[0], dagger.DirInput(args[1], []string{})) }, } diff --git a/cmd/dagger/cmd/input/git.go b/cmd/dagger/cmd/input/git.go index 3452f1a7..db0c75cd 100644 --- a/cmd/dagger/cmd/input/git.go +++ b/cmd/dagger/cmd/input/git.go @@ -1,14 +1,16 @@ package input import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" "github.com/spf13/cobra" "github.com/spf13/viper" ) var gitCmd = &cobra.Command{ - Use: "git REMOTE REF [SUBDIR]", + Use: "git TARGET REMOTE REF [SUBDIR]", Short: "Add a git repository as input artifact", - Args: cobra.MinimumNArgs(2), + Args: cobra.RangeArgs(3, 4), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -17,10 +19,15 @@ var gitCmd = &cobra.Command{ } }, Run: func(cmd *cobra.Command, args []string) { - // lg := logger.New() - // ctx := lg.WithContext(cmd.Context()) + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) - panic("not implemented") + subDir := "" + if len(args) > 3 { + subDir = args[3] + } + + updateRouteInput(ctx, args[0], dagger.GitInput(args[1], args[2], subDir)) }, } diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index a8cb9f06..62150429 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -1,6 +1,13 @@ package input -import "github.com/spf13/cobra" +import ( + "context" + + "dagger.io/go/cmd/dagger/cmd/common" + "dagger.io/go/dagger" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) // Cmd exposes the top-level command var Cmd = &cobra.Command{ @@ -14,6 +21,23 @@ func init() { gitCmd, containerCmd, secretCmd, - valueCmd, + textCmd, ) } + +func updateRouteInput(ctx context.Context, target string, input dagger.Input) { + lg := log.Ctx(ctx) + + store, err := dagger.DefaultStore() + if err != nil { + lg.Fatal().Err(err).Msg("failed to load store") + } + + st := common.GetCurrentRouteState(ctx, store) + st.AddInput(target, input) + + if err := store.UpdateRoute(ctx, st, nil); err != nil { + lg.Fatal().Err(err).Str("routeId", st.ID).Str("routeName", st.Name).Msg("cannot update route") + } + lg.Info().Str("routeId", st.ID).Str("routeName", st.Name).Msg("updated route") +} diff --git a/cmd/dagger/cmd/input/secret.go b/cmd/dagger/cmd/input/secret.go index b1abd078..7701d046 100644 --- a/cmd/dagger/cmd/input/secret.go +++ b/cmd/dagger/cmd/input/secret.go @@ -6,9 +6,9 @@ import ( ) var secretCmd = &cobra.Command{ - Use: "secret VALUE", + Use: "secret TARGET VALUE", Short: "Add an encrypted input secret", - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 diff --git a/cmd/dagger/cmd/input/value.go b/cmd/dagger/cmd/input/text.go similarity index 52% rename from cmd/dagger/cmd/input/value.go rename to cmd/dagger/cmd/input/text.go index f8573493..3f500680 100644 --- a/cmd/dagger/cmd/input/value.go +++ b/cmd/dagger/cmd/input/text.go @@ -1,14 +1,16 @@ package input import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" "github.com/spf13/cobra" "github.com/spf13/viper" ) -var valueCmd = &cobra.Command{ - Use: "value VALUE", - Short: "Add an input value", - Args: cobra.ExactArgs(1), +var textCmd = &cobra.Command{ + Use: "text TARGET VALUE", + Short: "Add an input text", + Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -17,15 +19,15 @@ var valueCmd = &cobra.Command{ } }, Run: func(cmd *cobra.Command, args []string) { - // lg := logger.New() - // ctx := lg.WithContext(cmd.Context()) + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) - panic("not implemented") + updateRouteInput(ctx, args[0], dagger.TextInput(args[1])) }, } func init() { - if err := viper.BindPFlags(valueCmd.Flags()); err != nil { + if err := viper.BindPFlags(textCmd.Flags()); err != nil { panic(err) } } diff --git a/cmd/dagger/cmd/new.go b/cmd/dagger/cmd/new.go index 7fd9e9b7..b5a83ae1 100644 --- a/cmd/dagger/cmd/new.go +++ b/cmd/dagger/cmd/new.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" + "dagger.io/go/cmd/dagger/cmd/common" "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" @@ -56,7 +57,7 @@ var newCmd = &cobra.Command{ } if viper.GetBool("up") { - routeUp(ctx, route) + common.RouteUp(ctx, route) } }, } diff --git a/cmd/dagger/cmd/query.go b/cmd/dagger/cmd/query.go index 0eae30de..8a37bc76 100644 --- a/cmd/dagger/cmd/query.go +++ b/cmd/dagger/cmd/query.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" + "dagger.io/go/cmd/dagger/cmd/common" "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" @@ -30,7 +31,7 @@ var queryCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load store") } - route := getCurrentRoute(ctx, store) + route := common.GetCurrentRoute(ctx, store) expr := args[0] diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 4ea07a3e..d47bbb6a 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -2,6 +2,7 @@ package cmd import ( + "dagger.io/go/cmd/dagger/cmd/common" "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" @@ -28,10 +29,10 @@ var upCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load store") } - route := getCurrentRoute(ctx, store) + route := common.GetCurrentRoute(ctx, store) // TODO: Implement options: --no-cache - routeUp(ctx, route) + common.RouteUp(ctx, route) }, }