Skip to content

Commit

Permalink
🎨 Extract log struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrudolph committed Feb 10, 2021
1 parent dc5bbd2 commit c33868f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
36 changes: 23 additions & 13 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ type credentials struct {
SecretAccessKey string
}

type log struct {
entries []string
}

func (l *log) Printf(format string, v ...interface{}) {
l.entries = append(l.entries, fmt.Sprintf(format, v...))
fmt.Printf(format, v...)
}

func (l *log) Text() string {
return strings.Join(l.entries, "\n")
}

// Submit represents the task of preparing and sending a set of test results to
// BuildPulse.
type Submit struct {
client *http.Client
diagnostics *log
fs *flag.FlagSet
idgen func() uuid.UUID
version *metadata.Version
diagnostics []string

envs map[string]string
path string
Expand All @@ -49,10 +62,11 @@ type Submit struct {
// NewSubmit creates a new Submit instance.
func NewSubmit(version *metadata.Version) *Submit {
s := &Submit{
client: http.DefaultClient,
fs: flag.NewFlagSet("submit", flag.ContinueOnError),
idgen: uuid.New,
version: version,
client: http.DefaultClient,
diagnostics: &log{},
fs: flag.NewFlagSet("submit", flag.ContinueOnError),
idgen: uuid.New,
version: version,
}

s.fs.Uint64Var(&s.accountID, "account-id", 0, "BuildPulse account ID (required)")
Expand All @@ -66,13 +80,13 @@ func NewSubmit(version *metadata.Version) *Submit {
// Init populates s from args and envs. It returns an error if the required args
// or environment variables are missing or malformed.
func (s *Submit) Init(args []string, envs map[string]string) error {
s.appendDiagnostics(fmt.Sprintf("args: %+v", args))
s.diagnostics.Printf("args: %+v", args)

dir, err := os.Getwd()
if err != nil {
return err
}
s.appendDiagnostics(fmt.Sprintf("working directory: %v", dir))
s.diagnostics.Printf("working directory: %v", dir)

s.path = args[0]
isFlag, err := regexp.MatchString("^-", s.path)
Expand Down Expand Up @@ -117,7 +131,7 @@ func (s *Submit) Init(args []string, envs map[string]string) error {
if err != nil {
// Git metadata functionality is experimental. While it's experimental, don't let an invalid repository prevent the test-reporter from continuing normal operation.
warning := fmt.Sprintf("[experimental] invalid value for flag -repository-dir: %v\n", err)
s.appendDiagnostics(fmt.Sprintf("warning: %v", warning))
s.diagnostics.Printf("warning: %v", warning)
fmt.Fprintf(os.Stderr, warning)
}

Expand All @@ -143,7 +157,7 @@ func (s *Submit) Run() (string, error) {
return "", err
}

err = ioutil.WriteFile(filepath.Join(s.path, "buildpulse.log"), []byte(strings.Join(s.diagnostics, "\n")), 0644)
err = ioutil.WriteFile(filepath.Join(s.path, "buildpulse.log"), []byte(s.diagnostics.Text()), 0644)
if err != nil {
return "", err
}
Expand All @@ -166,10 +180,6 @@ func (s *Submit) upload(path string) (string, error) {
return key, nil
}

func (s *Submit) appendDiagnostics(entry string) {
s.diagnostics = append(s.diagnostics, entry)
}

// toTarGz creates a gzipped tarball containing the contents of the named
// directory (dir) and returns the path of the resulting file.
func toTarGz(dir string) (dest string, err error) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func TestSubmit_Run(t *testing.T) {

s := &Submit{
client: &http.Client{Transport: r},
diagnostics: &log{},
idgen: func() uuid.UUID { return uuid.MustParse("00000000-0000-0000-0000-000000000000") },
version: &metadata.Version{Number: "v1.2.3"},
commitResolver: commitResolverDouble,
Expand Down Expand Up @@ -331,6 +332,7 @@ func Test_upload(t *testing.T) {

s := &Submit{
client: &http.Client{Transport: r},
diagnostics: &log{},
idgen: func() uuid.UUID { return uuid.MustParse("00000000-0000-0000-0000-000000000000") },
accountID: tt.accountID,
repositoryID: 8675309,
Expand Down

0 comments on commit c33868f

Please sign in to comment.