From a1c90413633df09cd7582b894efd277acc6df72e Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 26 Mar 2021 11:04:31 -0700 Subject: [PATCH] implement proper route lookup Signed-off-by: Sam Alba --- cmd/dagger/cmd/common.go | 39 +++++++++++++++++++------------ cmd/dagger/cmd/compute.go | 3 +-- cmd/dagger/cmd/down.go | 20 +++------------- cmd/dagger/cmd/new.go | 49 +++++++++++++++++++++++++++++++++++---- cmd/dagger/cmd/query.go | 21 +++-------------- cmd/dagger/cmd/up.go | 18 +------------- 6 files changed, 76 insertions(+), 74 deletions(-) diff --git a/cmd/dagger/cmd/common.go b/cmd/dagger/cmd/common.go index 21de7181..0f479395 100644 --- a/cmd/dagger/cmd/common.go +++ b/cmd/dagger/cmd/common.go @@ -4,36 +4,45 @@ import ( "context" "fmt" "os" - "path/filepath" "dagger.io/go/dagger" "github.com/rs/zerolog/log" "github.com/spf13/viper" ) -// getRouteName returns the selected route name (based on explicit CLI selection or current work dir) -func getRouteName(ctx context.Context) string { +// getCurrentRoute returns the current selected route based on its abs path +func getCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route { lg := log.Ctx(ctx) + var ( + st *dagger.RouteState + err error + ) + routeName := viper.GetString("route") if routeName != "" { - return routeName + st, err = store.LookupRouteByName(ctx, routeName) + if err != nil { + lg.Fatal().Err(err).Str("routeName", routeName).Msg("failed to lookup route by name") + } + } else { + wd, err := os.Getwd() + if err != nil { + lg.Fatal().Err(err).Msg("cannot get current working directory") + } + + st, err = store.LookupRouteByPath(ctx, wd) + if err != nil { + lg.Fatal().Err(err).Str("routePath", wd).Msg("failed to lookup route by path") + } } - workDir, err := os.Getwd() + route, err := dagger.NewRoute(st) if err != nil { - lg. - Fatal(). - Err(err). - Msg("failed to get current working dir") + lg.Fatal().Err(err).Interface("routeState", st).Msg("failed to init route") } - currentDir := filepath.Base(workDir) - if currentDir == "/" { - return "root" - } - - return currentDir + return route } func routeUp(ctx context.Context, route *dagger.Route) { diff --git a/cmd/dagger/cmd/compute.go b/cmd/dagger/cmd/compute.go index 6867cfb0..dc220c4b 100644 --- a/cmd/dagger/cmd/compute.go +++ b/cmd/dagger/cmd/compute.go @@ -31,10 +31,9 @@ var computeCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) - name := getRouteName(ctx) st := &dagger.RouteState{ ID: uuid.New().String(), - Name: name, + Name: "FIXME", LayoutSource: dagger.DirInput(args[0], []string{"*.cue", "cue.mod"}), } diff --git a/cmd/dagger/cmd/down.go b/cmd/dagger/cmd/down.go index 1b246d00..447b4c2a 100644 --- a/cmd/dagger/cmd/down.go +++ b/cmd/dagger/cmd/down.go @@ -23,34 +23,20 @@ var downCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { lg := logger.New() ctx := lg.WithContext(cmd.Context()) + store, err := dagger.DefaultStore() if err != nil { lg.Fatal().Err(err).Msg("failed to load store") } - routeName := getRouteName(ctx) - st, err := store.LookupRouteByName(ctx, routeName) - if err != nil { - lg. - Fatal(). - Err(err). - Str("routeName", routeName). - Msg("failed to lookup route") - } - route, err := dagger.NewRoute(st) - if err != nil { - lg. - Fatal(). - Err(err). - Msg("failed to initialize route") - } + route := getCurrentRoute(ctx, store) // TODO: Implement options: --no-cache if err := route.Down(ctx, nil); err != nil { lg. Fatal(). Err(err). - Str("routeName", routeName). + Str("routeName", route.Name()). Str("routeId", route.ID()). Msg("failed to up the route") } diff --git a/cmd/dagger/cmd/new.go b/cmd/dagger/cmd/new.go index 9ee4fd8c..7fd9e9b7 100644 --- a/cmd/dagger/cmd/new.go +++ b/cmd/dagger/cmd/new.go @@ -1,9 +1,14 @@ package cmd import ( + "context" + "os" + "path/filepath" + "dagger.io/go/cmd/dagger/logger" "dagger.io/go/dagger" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -27,13 +32,11 @@ var newCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load store") } - upRouteFlag := viper.GetBool("up") - st := &dagger.RouteState{ - Name: getRouteName(ctx), + Name: getNewRouteName(ctx), + LayoutSource: getLayoutSource(ctx), } - // TODO: Implement options: --layout-*, --setup err = store.CreateRoute(ctx, st) if err != nil { lg.Fatal().Err(err).Msg("failed to create route") @@ -52,12 +55,48 @@ var newCmd = &cobra.Command{ Msg("failed to initialize route") } - if upRouteFlag { + if viper.GetBool("up") { routeUp(ctx, route) } }, } +func getNewRouteName(ctx context.Context) string { + lg := log.Ctx(ctx) + + routeName := viper.GetString("route") + if routeName != "" { + return routeName + } + + workDir, err := os.Getwd() + if err != nil { + lg. + Fatal(). + Err(err). + Msg("failed to get current working dir") + } + + currentDir := filepath.Base(workDir) + if currentDir == "/" { + return "root" + } + + return currentDir +} + +// FIXME: Implement options: --layout-* +func getLayoutSource(ctx context.Context) dagger.Input { + lg := log.Ctx(ctx) + + wd, err := os.Getwd() + if err != nil { + lg.Fatal().Err(err).Msg("cannot get current working directory") + } + + return dagger.DirInput(wd, []string{"*.cue", "cue.mod"}) +} + func init() { newCmd.Flags().StringP("name", "n", "", "Specify a route name") newCmd.Flags().BoolP("up", "u", false, "Bring the route online") diff --git a/cmd/dagger/cmd/query.go b/cmd/dagger/cmd/query.go index 8bcfd86a..0eae30de 100644 --- a/cmd/dagger/cmd/query.go +++ b/cmd/dagger/cmd/query.go @@ -24,28 +24,13 @@ var queryCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { lg := logger.New() ctx := lg.WithContext(cmd.Context()) + store, err := dagger.DefaultStore() if err != nil { lg.Fatal().Err(err).Msg("failed to load store") } - routeName := getRouteName(ctx) - st, err := store.LookupRouteByName(ctx, routeName) - if err != nil { - lg. - Fatal(). - Err(err). - Str("routeName", routeName). - Msg("failed to lookup route") - } - - route, err := dagger.NewRoute(st) - if err != nil { - lg. - Fatal(). - Err(err). - Msg("failed to initialize route") - } + route := getCurrentRoute(ctx, store) expr := args[0] @@ -54,7 +39,7 @@ var queryCmd = &cobra.Command{ lg. Fatal(). Err(err). - Str("routeName", routeName). + Str("routeName", route.Name()). Str("routeId", route.ID()). Msg("failed to query route") } diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 94b07f2d..4ea07a3e 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -28,23 +28,7 @@ var upCmd = &cobra.Command{ lg.Fatal().Err(err).Msg("failed to load store") } - routeName := getRouteName(ctx) - st, err := store.LookupRouteByName(ctx, routeName) - if err != nil { - lg. - Fatal(). - Err(err). - Str("routeName", routeName). - Msg("failed to lookup route") - } - - route, err := dagger.NewRoute(st) - if err != nil { - lg. - Fatal(). - Err(err). - Msg("failed to initialize route") - } + route := getCurrentRoute(ctx, store) // TODO: Implement options: --no-cache routeUp(ctx, route)