implemented new, up, list

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-03-25 14:24:05 -07:00 committed by Solomon Hykes
parent 43956e38cc
commit 7ad541feb1
7 changed files with 56 additions and 29 deletions

View File

@ -1,9 +1,12 @@
package cmd package cmd
import ( import (
"context"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"dagger.io/go/dagger"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -31,3 +34,15 @@ func getRouteName(lg zerolog.Logger, cmd *cobra.Command) string {
return currentDir return currentDir
} }
func routeUp(ctx context.Context, lg zerolog.Logger, route *dagger.Route) {
c, err := dagger.NewClient(ctx, "")
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
output, err := c.Up(ctx, route)
if err != nil {
lg.Fatal().Err(err).Msg("failed to compute")
}
fmt.Println(output.JSON())
}

View File

@ -3,7 +3,6 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"os" "os"
"strings" "strings"
@ -121,15 +120,7 @@ var computeCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("unable to initialize route") lg.Fatal().Err(err).Msg("unable to initialize route")
} }
c, err := dagger.NewClient(ctx, "") routeUp(ctx, lg, route)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
output, err := c.Up(ctx, route)
if err != nil {
lg.Fatal().Err(err).Msg("failed to compute")
}
fmt.Println(output.JSON())
}, },
} }

View File

@ -1,6 +1,10 @@
package cmd package cmd
import ( import (
"fmt"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -17,10 +21,17 @@ var listCmd = &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())
panic("not implemented") routes, err := dagger.ListRoutes(ctx)
if err != nil {
lg.Fatal().Err(err).Msg("cannot list routes")
}
for _, name := range routes {
fmt.Println(name)
}
}, },
} }

View File

@ -24,7 +24,7 @@ var newCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
// nolint:staticcheck // nolint:staticcheck
upRoute, err := cmd.Flags().GetBool("up") upRouteFlag, err := cmd.Flags().GetBool("up")
if err != nil { if err != nil {
lg.Fatal().Err(err).Str("flag", "up").Msg("unable to resolve flag") lg.Fatal().Err(err).Str("flag", "up").Msg("unable to resolve flag")
} }
@ -39,12 +39,8 @@ var newCmd = &cobra.Command{
} }
lg.Info().Str("route-id", route.ID()).Str("route-name", routeName).Msg("created route") lg.Info().Str("route-id", route.ID()).Str("route-name", routeName).Msg("created route")
if upRoute { if upRouteFlag {
lg.Info().Str("route-id", route.ID()).Msg("bringing route online") routeUp(ctx, lg, route)
// FIXME
if err := route.FIXME(ctx); err != nil {
lg.Fatal().Err(err).Str("route-id", route.ID()).Msg("failed to create route")
}
} }
}, },
} }

View File

@ -32,10 +32,7 @@ var upCmd = &cobra.Command{
} }
// TODO: Implement options: --no-cache // TODO: Implement options: --no-cache
// FIXME routeUp(ctx, lg, route)
if err := route.FIXME(ctx); err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Str("route-id", route.ID()).Msg("failed to up the route")
}
}, },
} }

View File

@ -2,7 +2,6 @@ package dagger
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"strings" "strings"
@ -311,10 +310,6 @@ func (r *Route) Query(ctx context.Context, expr interface{}, o *QueryOpts) (*com
panic("NOT IMPLEMENTED") panic("NOT IMPLEMENTED")
} }
func (r *Route) FIXME(ctx context.Context) error {
return errors.New("FIXME")
}
type QueryOpts struct{} type QueryOpts struct{}
func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc { func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc {

View File

@ -4,8 +4,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil"
"os" "os"
"path" "path"
"strings"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -67,6 +69,26 @@ func LoadRoute(ctx context.Context, id string, o *LoadOpts) (*Route, error) {
panic("NOT IMPLEMENTED") panic("NOT IMPLEMENTED")
} }
func ListRoutes(ctx context.Context) ([]string, error) {
routes := []string{}
rootDir := os.ExpandEnv(storeLocation)
files, err := ioutil.ReadDir(rootDir)
if err != nil {
return nil, err
}
for _, f := range files {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") {
// There is extra data in the directory, ignore
continue
}
routes = append(routes, f.Name()[:len(f.Name())-5])
}
return routes, nil
}
func routePath(name string) string { func routePath(name string) string {
return path.Join(os.ExpandEnv(storeLocation), name+".json") return path.Join(os.ExpandEnv(storeLocation), name+".json")
} }