implement proper route lookup
Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
parent
e08e64b311
commit
a1c9041363
@ -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
|
||||
}
|
||||
|
||||
workDir, err := os.Getwd()
|
||||
st, err = store.LookupRouteByName(ctx, routeName)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
Err(err).
|
||||
Msg("failed to get current working dir")
|
||||
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")
|
||||
}
|
||||
|
||||
currentDir := filepath.Base(workDir)
|
||||
if currentDir == "/" {
|
||||
return "root"
|
||||
st, err = store.LookupRouteByPath(ctx, wd)
|
||||
if err != nil {
|
||||
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) {
|
||||
|
@ -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"}),
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user