-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create and update template from file
- Loading branch information
Yuanlin Lin
committed
Jun 5, 2024
1 parent
2dfb579
commit e7c6288
Showing
6 changed files
with
244 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package create | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/zeabur/cli/internal/cmdutil" | ||
) | ||
|
||
type Options struct { | ||
file string | ||
} | ||
|
||
func NewCmdCreate(f *cmdutil.Factory) *cobra.Command { | ||
opts := Options{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create template from file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runCreate(f, opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.file, "file", "f", "", "Template file") | ||
|
||
return cmd | ||
} | ||
|
||
func runCreate(f *cmdutil.Factory, opts Options) error { | ||
if opts.file == "" { | ||
return fmt.Errorf("file is required, use -f or --file to specify the file path") | ||
} | ||
|
||
var file []byte | ||
var err error | ||
|
||
if strings.HasPrefix(opts.file, "https://") || strings.HasPrefix(opts.file, "http://") { | ||
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval, | ||
spinner.WithColor(cmdutil.SpinnerColor), | ||
spinner.WithSuffix(" Fetching remote template file ..."), | ||
) | ||
|
||
s.Start() | ||
get, err := http.Get(opts.file) | ||
if err != nil { | ||
f.Log.Errorf("fetch file failed: %v", err) | ||
return err | ||
} | ||
|
||
file, err = io.ReadAll(get.Body) | ||
if err != nil { | ||
f.Log.Errorf("read file failed: %v", err) | ||
return err | ||
} | ||
|
||
s.Stop() | ||
} else { | ||
file, err = os.ReadFile(opts.file) | ||
if err != nil { | ||
return fmt.Errorf("read file failed: %w", err) | ||
} | ||
} | ||
|
||
type RawTemplate struct { | ||
APIVersion string `yaml:"apiVersion"` | ||
Kind string `yaml:"kind"` | ||
Metadata struct { | ||
Name string `yaml:"name"` | ||
} `yaml:"metadata"` | ||
Spec struct { | ||
Variables []struct { | ||
Key string `yaml:"key"` | ||
Type string `yaml:"type"` | ||
Name string `yaml:"name"` | ||
Description string `yaml:"description"` | ||
} `yaml:"variables"` | ||
} | ||
} | ||
|
||
var raw RawTemplate | ||
err = yaml.Unmarshal(file, &raw) | ||
if err != nil { | ||
return fmt.Errorf("parse yaml failed: %w", err) | ||
} | ||
|
||
t, err := f.ApiClient.CreateTemplateFromFile(context.Background(), string(file)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f.Log.Infof("Template %q (https://zeabur.com/templates/%s) created", t.Name, t.Code) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package update | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/zeabur/cli/internal/cmdutil" | ||
) | ||
|
||
type Options struct { | ||
code string | ||
file string | ||
} | ||
|
||
func NewCmdUpdate(f *cmdutil.Factory) *cobra.Command { | ||
opts := Options{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update template from file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runUpdate(f, opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.code, "code", "c", "", "Template code") | ||
cmd.Flags().StringVarP(&opts.file, "file", "f", "", "Template file") | ||
|
||
return cmd | ||
} | ||
|
||
func runUpdate(f *cmdutil.Factory, opts Options) error { | ||
if opts.code == "" { | ||
return fmt.Errorf("code is required, use -c or --code to specify the template code") | ||
} | ||
|
||
if opts.file == "" { | ||
return fmt.Errorf("file is required, use -f or --file to specify the file path") | ||
} | ||
|
||
var file []byte | ||
var err error | ||
|
||
if strings.HasPrefix(opts.file, "https://") || strings.HasPrefix(opts.file, "http://") { | ||
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval, | ||
spinner.WithColor(cmdutil.SpinnerColor), | ||
spinner.WithSuffix(" Fetching remote template file ..."), | ||
) | ||
|
||
s.Start() | ||
get, err := http.Get(opts.file) | ||
if err != nil { | ||
f.Log.Errorf("fetch file failed: %v", err) | ||
return err | ||
} | ||
|
||
file, err = io.ReadAll(get.Body) | ||
if err != nil { | ||
f.Log.Errorf("read file failed: %v", err) | ||
return err | ||
} | ||
|
||
s.Stop() | ||
} else { | ||
file, err = os.ReadFile(opts.file) | ||
if err != nil { | ||
return fmt.Errorf("read file failed: %w", err) | ||
} | ||
} | ||
|
||
type RawTemplate struct { | ||
APIVersion string `yaml:"apiVersion"` | ||
Kind string `yaml:"kind"` | ||
Metadata struct { | ||
Name string `yaml:"name"` | ||
} `yaml:"metadata"` | ||
Spec struct { | ||
Variables []struct { | ||
Key string `yaml:"key"` | ||
Type string `yaml:"type"` | ||
Name string `yaml:"name"` | ||
Description string `yaml:"description"` | ||
} `yaml:"variables"` | ||
} | ||
} | ||
|
||
var raw RawTemplate | ||
err = yaml.Unmarshal(file, &raw) | ||
if err != nil { | ||
return fmt.Errorf("parse yaml failed: %w", err) | ||
} | ||
|
||
_, err = f.ApiClient.UpdateTemplateFromFile(context.Background(), opts.code, string(file)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f.Log.Infof("Template updated.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters