Skip to content

Commit

Permalink
Merge pull request #1796 from carolynvs/print-input-file
Browse files Browse the repository at this point in the history
Print applied file in debug mode
  • Loading branch information
carolynvs authored Oct 13, 2021
2 parents e5e397a + f9eb05b commit 4b2126c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/credentials/credentialset.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (s CredentialSet) Validate() error {
return nil
}

func (s CredentialSet) String() string {
return fmt.Sprintf("%s/%s", s.Namespace, s.Name)
}

// Validate compares the given credentials with the spec.
//
// This will result in an error only when the following conditions are true:
Expand Down
12 changes: 12 additions & 0 deletions pkg/credentials/credentialset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ func TestMarshal(t *testing.T) {
})
}
}

func TestCredentialSet_String(t *testing.T) {
t.Run("global namespace", func(t *testing.T) {
cs := CredentialSet{Name: "mycreds"}
assert.Equal(t, "/mycreds", cs.String())
})

t.Run("local namespace", func(t *testing.T) {
cs := CredentialSet{Namespace: "dev", Name: "mycreds"}
assert.Equal(t, "dev/mycreds", cs.String())
})
}
5 changes: 5 additions & 0 deletions pkg/parameters/parameterset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parameters

import (
"fmt"
"time"

"get.porter.sh/porter/pkg/secrets"
Expand Down Expand Up @@ -61,3 +62,7 @@ func (s ParameterSet) Validate() error {
}
return nil
}

func (s ParameterSet) String() string {
return fmt.Sprintf("%s/%s", s.Namespace, s.Name)
}
12 changes: 12 additions & 0 deletions pkg/parameters/parameterset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ func TestNewParameterSet(t *testing.T) {
assert.Equal(t, SchemaVersion, cs.SchemaVersion, "SchemaVersion was not set")
assert.Len(t, cs.Parameters, 1, "Parameters should be initialized with 1 value")
}

func TestParameterSet_String(t *testing.T) {
t.Run("global namespace", func(t *testing.T) {
ps := ParameterSet{Name: "myparams"}
assert.Equal(t, "/myparams", ps.String())
})

t.Run("local namespace", func(t *testing.T) {
ps := ParameterSet{Namespace: "dev", Name: "myparams"}
assert.Equal(t, "dev/myparams", ps.String())
})
}
10 changes: 10 additions & 0 deletions pkg/porter/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ func (o *ApplyOptions) Validate(cxt *context.Context, args []string) error {
}

func (p *Porter) InstallationApply(opts ApplyOptions) error {
if p.Debug {
fmt.Fprintf(p.Err, "Reading input file %s...\n", opts.File)
}

namespace, err := p.getNamespaceFromFile(opts)
if err != nil {
return err
}

if p.Debug {
// ignoring any error here, printing debug info isn't critical
contents, _ := p.FileSystem.ReadFile(opts.File)
fmt.Fprintf(p.Err, "Input file contents:\n%s\n", contents)
}

var input claims.Installation
if err := encoding.UnmarshalFile(p.FileSystem, opts.File, &input); err != nil {
return errors.Wrapf(err, "unable to parse %s as an installation document", opts.File)
Expand Down
18 changes: 17 additions & 1 deletion pkg/porter/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,21 @@ func validateCredentialName(args []string) error {
}

func (p *Porter) CredentialsApply(o ApplyOptions) error {
if p.Debug {
fmt.Fprintf(p.Err, "Reading input file %s...\n", o.File)
}

namespace, err := p.getNamespaceFromFile(o)
if err != nil {
return err
}

if p.Debug {
// ignoring any error here, printing debug info isn't critical
contents, _ := p.FileSystem.ReadFile(o.File)
fmt.Fprintf(p.Err, "Input file contents:\n%s\n", contents)
}

var creds credentials.CredentialSet
err = encoding.UnmarshalFile(p.FileSystem, o.File, &creds)
if err != nil {
Expand All @@ -324,7 +334,13 @@ func (p *Porter) CredentialsApply(o ApplyOptions) error {
return errors.Wrap(err, "credential set is invalid")
}

return p.Credentials.UpsertCredentialSet(creds)
err = p.Credentials.UpsertCredentialSet(creds)
if err != nil {
return err
}

fmt.Fprintf(p.Err, "Applied %s credential set\n", creds)
return nil
}

func (p *Porter) getNamespaceFromFile(o ApplyOptions) (string, error) {
Expand Down
18 changes: 17 additions & 1 deletion pkg/porter/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,21 @@ func (p *Porter) printDisplayValuesTable(values []DisplayValue) error {
}

func (p *Porter) ParametersApply(o ApplyOptions) error {
if p.Debug {
fmt.Fprintf(p.Err, "Reading input file %s...\n", o.File)
}

namespace, err := p.getNamespaceFromFile(o)
if err != nil {
return err
}

if p.Debug {
// ignoring any error here, printing debug info isn't critical
contents, _ := p.FileSystem.ReadFile(o.File)
fmt.Fprintf(p.Err, "Input file contents:\n%s\n", contents)
}

var params parameters.ParameterSet
err = encoding.UnmarshalFile(p.FileSystem, o.File, &params)
if err != nil {
Expand All @@ -499,7 +509,13 @@ func (p *Porter) ParametersApply(o ApplyOptions) error {
return errors.Wrap(err, "parameter set is invalid")
}

return p.Parameters.UpsertParameterSet(params)
err = p.Parameters.UpsertParameterSet(params)
if err != nil {
return err
}

fmt.Fprintf(p.Err, "Applied %s parameter set\n", params)
return nil
}

// resolveParameters accepts a set of parameter assignments and combines them
Expand Down

0 comments on commit 4b2126c

Please sign in to comment.