Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Add validate command
Browse files Browse the repository at this point in the history
  • Loading branch information
langma committed Sep 4, 2020
1 parent 4798014 commit 60e587c
Show file tree
Hide file tree
Showing 10 changed files with 899 additions and 78 deletions.
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ as the active model for the application.`,
}

// open a stream for upload
stream, err := client.UploadTrainingData(ctx)
stream, err := config_client.UploadTrainingData(ctx)
if err != nil {
log.Fatalf("Failed to open deploy stream: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var describeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
appId, _ := cmd.Flags().GetString("app")
app, err := client.GetApp(ctx, &configv1.GetAppRequest{AppId: appId})
app, err := config_client.GetApp(ctx, &configv1.GetAppRequest{AppId: appId})
if err != nil {
log.Fatalf("Failed to get app %s: %s", appId, err)
}
Expand Down Expand Up @@ -49,13 +49,13 @@ var describeCmd = &cobra.Command{

func waitForDeploymentFinished(ctx context.Context, appId string) {
time.Sleep(5 * time.Second)
app, err := client.GetApp(ctx, &configv1.GetAppRequest{AppId: appId})
app, err := config_client.GetApp(ctx, &configv1.GetAppRequest{AppId: appId})
if err != nil {
log.Fatalf("Failed to get app %s: %s", appId, err)
}

for app.App.Status == configv1.App_STATUS_TRAINING {
app, err = client.GetApp(ctx, &configv1.GetAppRequest{AppId: appId})
app, err = config_client.GetApp(ctx, &configv1.GetAppRequest{AppId: appId})
if err != nil {
log.Fatalf("Failed to refresh app %s: %s", appId, err)
}
Expand Down
61 changes: 0 additions & 61 deletions cmd/download.go

This file was deleted.

6 changes: 3 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
)

var listCmd = &cobra.Command{
Use: "list",
Use: "list",
Short: "List applications in the current context (project)",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
projects, err := client.GetProject(ctx, &configv1.GetProjectRequest{})
projects, err := config_client.GetProject(ctx, &configv1.GetProjectRequest{})
if err != nil {
log.Fatalf("Getting projects failed: %s", err)
}
project := projects.Project[0]
apps, err := client.ListApps(ctx, &configv1.ListAppsRequest{Project: project})
apps, err := config_client.ListApps(ctx, &configv1.ListAppsRequest{Project: project})
if err != nil {
log.Fatalf("Listing apps for project %s failed: %s", project, err)
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

homedir "github.com/mitchellh/go-homedir"
configv1 "github.com/speechly/cli/gen/go/speechly/config/v1"
compilev1 "github.com/speechly/cli/gen/go/speechly/sal/v1"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
Expand All @@ -30,10 +31,11 @@ type SpeechlyContext struct {
}

var (
client configv1.ConfigAPIClient
conf Config
sc SpeechlyContext
rootCmd = &cobra.Command{
config_client configv1.ConfigAPIClient
compile_client compilev1.CompilerClient
conf Config
sc SpeechlyContext
rootCmd = &cobra.Command{
Use: "speechly",
Short: "Speechly API Client",
Long: logo,
Expand Down Expand Up @@ -114,6 +116,8 @@ func Execute() error {
ServerName: sc.Host,
})
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}

conn, err := grpc.DialContext(ctx, serverAddr, opts...)
Expand All @@ -122,7 +126,8 @@ func Execute() error {
}
defer conn.Close()

client = configv1.NewConfigAPIClient(conn)
config_client = configv1.NewConfigAPIClient(conn)
compile_client = compilev1.NewCompilerClient(conn)

return rootCmd.ExecuteContext(ctx)
}
94 changes: 94 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cmd

import (
"log"
"os"
"path/filepath"

"github.com/spf13/cobra"

compilev1 "github.com/speechly/cli/gen/go/speechly/sal/v1"
)

type CompileWriter struct {
appId string
stream compilev1.Compiler_ValidateClient
}

func (u CompileWriter) Write(data []byte) (n int, err error) {
req := &compilev1.AppSource{AppId: u.appId, DataChunk: data}
if err = u.stream.Send(req); err != nil {
return 0, err
}
return len(data), nil
}

var validateCmd = &cobra.Command{
Use: "validate [directory]",
Example: `speechly validate -a UUID_APP_ID .
speechly validate -a UUID_APP_ID /usr/local/project/app`,
Short: "Validate the given configuration for syntax errors",
Long: `The contents of the directory given as argument is sent to the
API and validated. Possible errors are printed to stdout.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
appId, _ := cmd.Flags().GetString("app")
inDir := args[0]
absPath, _ := filepath.Abs(inDir)
log.Printf("Project dir: %s\n", absPath)
// create a tar package from files in memory
uploadData := createTarFromDir(inDir)

if len(uploadData.files) == 0 {
log.Fatalf("No files found for validation!\n\nPlease ensure the files are named *.yaml or *.csv")
}

// open a stream for upload
stream, err := compile_client.Validate(ctx)
if err != nil {
log.Fatalf("Failed to open validate stream: %s", err)
}

// flush the tar from memory to the stream
validateWriter := CompileWriter{appId, stream}
_, err = uploadData.buf.WriteTo(validateWriter)
if err != nil {
log.Fatalf("Streaming file data failed: %s", err)
}

validateResult, err := stream.CloseAndRecv()
if err != nil {
log.Fatalf("Validate failed: %s", err)
}
if len(validateResult.Messages) > 0 {
log.Println("Configuration validation failed")
for _, message := range validateResult.Messages {
var errorLevel string
switch message.Level {
case compilev1.LineReference_LEVEL_NOTE:
errorLevel = "NOTE"
case compilev1.LineReference_LEVEL_WARNING:
errorLevel = "WARNING"
case compilev1.LineReference_LEVEL_ERROR:
errorLevel = "ERROR"
}
if message.File != "" {
log.Printf("%s:%d:%d:%s:%s\n", message.File, message.Line,
message.Column, errorLevel, message.Message)
} else {
log.Printf("%s: %s", errorLevel, message.Message)
}
}
os.Exit(1)
} else {
log.Println("Configuration OK.")
}
},
}

func init() {
rootCmd.AddCommand(validateCmd)
validateCmd.Flags().StringP("app", "a", "", "application to deploy the files to.")
validateCmd.MarkFlagRequired("app")
}
Loading

0 comments on commit 60e587c

Please sign in to comment.