implement proper route lookup

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-03-26 11:04:31 -07:00
parent e08e64b311
commit a1c9041363
6 changed files with 76 additions and 74 deletions

View File

@ -4,36 +4,45 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"dagger.io/go/dagger" "dagger.io/go/dagger"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// getRouteName returns the selected route name (based on explicit CLI selection or current work dir) // getCurrentRoute returns the current selected route based on its abs path
func getRouteName(ctx context.Context) string { func getCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
var (
st *dagger.RouteState
err error
)
routeName := viper.GetString("route") routeName := viper.GetString("route")
if routeName != "" { if routeName != "" {
return routeName st, err = store.LookupRouteByName(ctx, routeName)
}
workDir, err := os.Getwd()
if err != nil { if err != nil {
lg. lg.Fatal().Err(err).Str("routeName", routeName).Msg("failed to lookup route by name")
Fatal(). }
Err(err). } else {
Msg("failed to get current working dir") wd, err := os.Getwd()
if err != nil {
lg.Fatal().Err(err).Msg("cannot get current working directory")
} }
currentDir := filepath.Base(workDir) st, err = store.LookupRouteByPath(ctx, wd)
if currentDir == "/" { if err != nil {
return "root" lg.Fatal().Err(err).Str("routePath", wd).Msg("failed to lookup route by path")
}
} }
return currentDir route, err := dagger.NewRoute(st)
if err != nil {
lg.Fatal().Err(err).Interface("routeState", st).Msg("failed to init route")
}
return route
} }
func routeUp(ctx context.Context, route *dagger.Route) { func routeUp(ctx context.Context, route *dagger.Route) {

View File

@ -31,10 +31,9 @@ var computeCmd = &cobra.Command{
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
name := getRouteName(ctx)
st := &dagger.RouteState{ st := &dagger.RouteState{
ID: uuid.New().String(), ID: uuid.New().String(),
Name: name, Name: "FIXME",
LayoutSource: dagger.DirInput(args[0], []string{"*.cue", "cue.mod"}), LayoutSource: dagger.DirInput(args[0], []string{"*.cue", "cue.mod"}),
} }

View File

@ -23,34 +23,20 @@ var downCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store, err := dagger.DefaultStore() store, err := dagger.DefaultStore()
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
routeName := getRouteName(ctx) route := getCurrentRoute(ctx, store)
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")
}
// TODO: Implement options: --no-cache // TODO: Implement options: --no-cache
if err := route.Down(ctx, nil); err != nil { if err := route.Down(ctx, nil); err != nil {
lg. lg.
Fatal(). Fatal().
Err(err). Err(err).
Str("routeName", routeName). Str("routeName", route.Name()).
Str("routeId", route.ID()). Str("routeId", route.ID()).
Msg("failed to up the route") Msg("failed to up the route")
} }

View File

@ -1,9 +1,14 @@
package cmd package cmd
import ( import (
"context"
"os"
"path/filepath"
"dagger.io/go/cmd/dagger/logger" "dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger" "dagger.io/go/dagger"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -27,13 +32,11 @@ var newCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
upRouteFlag := viper.GetBool("up")
st := &dagger.RouteState{ st := &dagger.RouteState{
Name: getRouteName(ctx), Name: getNewRouteName(ctx),
LayoutSource: getLayoutSource(ctx),
} }
// TODO: Implement options: --layout-*, --setup
err = store.CreateRoute(ctx, st) err = store.CreateRoute(ctx, st)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to create route") lg.Fatal().Err(err).Msg("failed to create route")
@ -52,12 +55,48 @@ var newCmd = &cobra.Command{
Msg("failed to initialize route") Msg("failed to initialize route")
} }
if upRouteFlag { if viper.GetBool("up") {
routeUp(ctx, route) 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() { func init() {
newCmd.Flags().StringP("name", "n", "", "Specify a route name") newCmd.Flags().StringP("name", "n", "", "Specify a route name")
newCmd.Flags().BoolP("up", "u", false, "Bring the route online") newCmd.Flags().BoolP("up", "u", false, "Bring the route online")

View File

@ -24,28 +24,13 @@ var queryCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store, err := dagger.DefaultStore() store, err := dagger.DefaultStore()
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
routeName := getRouteName(ctx) route := getCurrentRoute(ctx, store)
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")
}
expr := args[0] expr := args[0]
@ -54,7 +39,7 @@ var queryCmd = &cobra.Command{
lg. lg.
Fatal(). Fatal().
Err(err). Err(err).
Str("routeName", routeName). Str("routeName", route.Name()).
Str("routeId", route.ID()). Str("routeId", route.ID()).
Msg("failed to query route") Msg("failed to query route")
} }

View File

@ -28,23 +28,7 @@ var upCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("failed to load store") lg.Fatal().Err(err).Msg("failed to load store")
} }
routeName := getRouteName(ctx) route := getCurrentRoute(ctx, store)
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")
}
// TODO: Implement options: --no-cache // TODO: Implement options: --no-cache
routeUp(ctx, route) routeUp(ctx, route)