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
import (
"context"
"fmt"
"os"
"path/filepath"
"dagger.io/go/dagger"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
@ -31,3 +34,15 @@ func getRouteName(lg zerolog.Logger, cmd *cobra.Command) string {
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 (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
@ -121,15 +120,7 @@ var computeCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("unable to initialize 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())
routeUp(ctx, lg, route)
},
}

View File

@ -1,6 +1,10 @@
package cmd
import (
"fmt"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -17,10 +21,17 @@ var listCmd = &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")
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())
// nolint:staticcheck
upRoute, err := cmd.Flags().GetBool("up")
upRouteFlag, err := cmd.Flags().GetBool("up")
if err != nil {
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")
if upRoute {
lg.Info().Str("route-id", route.ID()).Msg("bringing route online")
// FIXME
if err := route.FIXME(ctx); err != nil {
lg.Fatal().Err(err).Str("route-id", route.ID()).Msg("failed to create route")
}
if upRouteFlag {
routeUp(ctx, lg, route)
}
},
}

View File

@ -32,10 +32,7 @@ var upCmd = &cobra.Command{
}
// TODO: Implement options: --no-cache
// FIXME
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")
}
routeUp(ctx, lg, route)
},
}

View File

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

View File

@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"os"
"path"
"strings"
"github.com/google/uuid"
)
@ -67,6 +69,26 @@ func LoadRoute(ctx context.Context, id string, o *LoadOpts) (*Route, error) {
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 {
return path.Join(os.ExpandEnv(storeLocation), name+".json")
}