Skip to content

Commit c2e1b76

Browse files
authored
src: direct serialization of Function metadata as func.yaml (#641)
* src: directly serialize Function metadata as func.yaml Functions now save directly to func.yaml using .Write(). Fixes a serialization error where defaults were not respected on load. Moves runtime and template defaults into function constructor. Extracts Function validation (was config validation) into separate functions. Extracts associated test files (validation) into separate unit test files. Updates schema generator to use Function * comment spelling and re-enabling tests
1 parent 3935747 commit c2e1b76

24 files changed

Lines changed: 2118 additions & 2067 deletions

client.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ func (c *Client) New(ctx context.Context, cfg Function) (err error) {
430430

431431
// Create a new Function project locally using the settings provided on a
432432
// Function object.
433-
func (c *Client) Create(f Function) (err error) {
433+
func (c *Client) Create(cfg Function) (err error) {
434434
// Create project root directory, if it doesn't already exist
435-
if err = os.MkdirAll(f.Root, 0755); err != nil {
435+
if err = os.MkdirAll(cfg.Root, 0755); err != nil {
436436
return
437437
}
438438

@@ -442,11 +442,11 @@ func (c *Client) Create(f Function) (err error) {
442442
// immediately exit with error (prior to actual creation) if this is
443443
// a Function already initialized at that path (Create should never
444444
// clobber a pre-existing Function)
445-
defaults, err := NewFunction(f.Root)
445+
f, err := NewFunctionFromDefaults(cfg)
446446
if err != nil {
447447
return
448448
}
449-
if defaults.Initialized() {
449+
if f.Initialized() {
450450
err = fmt.Errorf("Function at '%v' already initialized", f.Root)
451451
return
452452
}
@@ -463,16 +463,6 @@ func (c *Client) Create(f Function) (err error) {
463463
return
464464
}
465465

466-
// Assert runtime was provided, or default.
467-
if f.Runtime == "" {
468-
f.Runtime = DefaultRuntime
469-
}
470-
471-
// Assert template name was provided, or default.
472-
if f.Template == "" {
473-
f.Template = DefaultTemplate
474-
}
475-
476466
// Write out the template for a Function
477467
// returns a Function which may be mutated based on the content of
478468
// the template (default Function, builders, buildpacks, etc).
@@ -483,7 +473,7 @@ func (c *Client) Create(f Function) (err error) {
483473

484474
// Mark it as having been created via this client library and Write (save)
485475
f.Created = time.Now()
486-
if err = writeConfig(f); err != nil {
476+
if err = f.Write(); err != nil {
487477
return
488478
}
489479

@@ -544,9 +534,9 @@ func (c *Client) Build(ctx context.Context, path string) (err error) {
544534
return
545535
}
546536

547-
// Write out config, which will now contain a populated image tag
548-
// if it had not already
549-
if err = writeConfig(f); err != nil {
537+
// Write (save) - Serialize the Function to disk
538+
// Will now contain populated image tag.
539+
if err = f.Write(); err != nil {
550540
return
551541
}
552542

@@ -585,9 +575,9 @@ func (c *Client) Deploy(ctx context.Context, path string) (err error) {
585575
return
586576
}
587577

588-
// Store the produced image Digest in the config
578+
// Record the Image Digest pushed.
589579
f.ImageDigest = imageDigest
590-
if err = writeConfig(f); err != nil {
580+
if err = f.Write(); err != nil {
591581
return
592582
}
593583

client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestNew(t *testing.T) {
3838
root := "testdata/example.com/testNew"
3939
defer Using(t, root)()
4040

41-
client := fn.New(fn.WithRegistry(TestRegistry))
41+
client := fn.New(fn.WithRegistry(TestRegistry), fn.WithVerbose(true))
4242

4343
if err := client.New(context.Background(), fn.Function{Root: root}); err != nil {
4444
t.Fatal(err)
@@ -58,13 +58,13 @@ func TestWritesTemplate(t *testing.T) {
5858
}
5959

6060
// Assert the standard config file was written
61-
if _, err := os.Stat(filepath.Join(root, fn.ConfigFile)); os.IsNotExist(err) {
62-
t.Fatalf("Initialize did not result in '%v' being written to '%v'", fn.ConfigFile, root)
61+
if _, err := os.Stat(filepath.Join(root, fn.FunctionFile)); os.IsNotExist(err) {
62+
t.Fatalf("Initialize did not result in '%v' being written to '%v'", fn.FunctionFile, root)
6363
}
6464

6565
// Assert a file from the template was written
6666
if _, err := os.Stat(filepath.Join(root, "README.md")); os.IsNotExist(err) {
67-
t.Fatalf("Initialize did not result in '%v' being written to '%v'", fn.ConfigFile, root)
67+
t.Fatalf("Initialize did not result in '%v' being written to '%v'", fn.FunctionFile, root)
6868
}
6969
}
7070

cmd/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func runBuild(cmd *cobra.Command, _ []string, clientFn buildClientFn) (err error
145145
}
146146

147147
// All set, let's write changes in the config to the disk
148-
err = function.WriteConfig()
148+
err = function.Write()
149149
if err != nil {
150150
return
151151
}

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (s standardLoaderSaver) Load(path string) (fn.Function, error) {
3838
}
3939

4040
func (s standardLoaderSaver) Save(f fn.Function) error {
41-
return f.WriteConfig()
41+
return f.Write()
4242
}
4343

4444
var defaultLoaderSaver standardLoaderSaver

cmd/config_envs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func runAddEnvsPrompt(ctx context.Context, f fn.Function) (err error) {
375375
f.Envs[insertToIndex] = newEnv
376376
}
377377

378-
err = f.WriteConfig()
378+
err = f.Write()
379379
if err == nil {
380380
fmt.Println("Environment variable entry was added to the function configuration")
381381
}
@@ -419,7 +419,7 @@ func runRemoveEnvsPrompt(f fn.Function) (err error) {
419419

420420
if removed {
421421
f.Envs = newEnvs
422-
err = f.WriteConfig()
422+
err = f.Write()
423423
if err == nil {
424424
fmt.Println("Environment variable entry was removed from the function configuration")
425425
}

cmd/config_volumes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func runAddVolumesPrompt(ctx context.Context, f fn.Function) (err error) {
192192

193193
f.Volumes = append(f.Volumes, newVolume)
194194

195-
err = f.WriteConfig()
195+
err = f.Write()
196196
if err == nil {
197197
fmt.Println("Volume entry was added to the function configuration")
198198
}
@@ -236,7 +236,7 @@ func runRemoveVolumesPrompt(f fn.Function) (err error) {
236236

237237
if removed {
238238
f.Volumes = newVolumes
239-
err = f.WriteConfig()
239+
err = f.Write()
240240
if err == nil {
241241
fmt.Println("Volume entry was removed from the function configuration")
242242
}

cmd/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func runDeploy(cmd *cobra.Command, _ []string, clientFn deployClientFn) (err err
158158
}
159159

160160
// All set, let's write changes in the config to the disk
161-
err = function.WriteConfig()
161+
err = function.Write()
162162
if err != nil {
163163
return
164164
}

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func runRun(cmd *cobra.Command, args []string, clientFn runClientFn) (err error)
7575
return
7676
}
7777

78-
err = function.WriteConfig()
78+
err = function.Write()
7979
if err != nil {
8080
return
8181
}

0 commit comments

Comments
 (0)