Skip to content

Commit

Permalink
Merge pull request #217 from kanisterio/chron-art-file
Browse files Browse the repository at this point in the history
Use a file to specify the chronicle artifact prefix
  • Loading branch information
tdmanv authored Aug 21, 2019
2 parents f451155 + f11f18d commit 809a5d0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
12 changes: 9 additions & 3 deletions pkg/chronicle/chronicle_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type PushParams struct {
ProfilePath string
ArtifactPath string
ArtifactFile string
Frequency time.Duration
EnvDir string
Command []string
Expand Down Expand Up @@ -85,8 +85,9 @@ func push(ctx context.Context, p PushParams, ord int) error {
return err
}
}
ap, err := readArtifactPathFile(p.ArtifactFile)
log.Debugf("Pushing output from Command %d: %v. Environment: %v", ord, p.Command, env)
return pushWithEnv(ctx, p.Command, p.ArtifactPath, ord, prof, env)
return pushWithEnv(ctx, p.Command, ap, ord, prof, env)
}

func pushWithEnv(ctx context.Context, c []string, suffix string, ord int, prof param.Profile, env []string) error {
Expand Down Expand Up @@ -121,12 +122,17 @@ func pushWithEnv(ctx context.Context, c []string, suffix string, ord int, prof p
return nil
}

func readArtifactPathFile(path string) (string, error) {
buf, err := ioutil.ReadFile(path)
t := strings.TrimSuffix(string(buf), "\n")
return t, errors.Wrap(err, "Could not read artifact path file")
}

func readProfile(path string) (p param.Profile, ok bool, err error) {
var buf []byte
buf, err = ioutil.ReadFile(path)
switch {
case os.IsNotExist(err):
ok = true
err = nil
return
case err != nil:
Expand Down
7 changes: 6 additions & 1 deletion pkg/chronicle/chronicle_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chronicle

import (
"context"
"io/ioutil"
"os"
"path/filepath"

. "gopkg.in/check.v1"
Expand All @@ -28,9 +30,12 @@ func (s *ChroniclePushSuite) TestPush(c *C) {
err := writeProfile(pp, prof)
c.Assert(err, IsNil)

a := filepath.Join(c.MkDir(), "artifact")
err = ioutil.WriteFile(a, []byte(rand.String(10)), os.ModePerm)
c.Assert(err, IsNil)
p := PushParams{
ProfilePath: pp,
ArtifactPath: rand.String(10),
ArtifactFile: a,
Command: []string{"echo hello"},
}
ctx := context.Background()
Expand Down
8 changes: 6 additions & 2 deletions pkg/chronicle/chronicle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -42,9 +43,12 @@ func (s *ChronicleSuite) TestPushPull(c *C) {
err := writeProfile(pp, s.profile)
c.Assert(err, IsNil)

a := filepath.Join(c.MkDir(), "artifact")
err = ioutil.WriteFile(a, []byte(rand.String(10)), os.ModePerm)
c.Assert(err, IsNil)
p := PushParams{
ProfilePath: pp,
ArtifactPath: rand.String(10),
ArtifactFile: a,
}
ctx := context.Background()

Expand All @@ -56,7 +60,7 @@ func (s *ChronicleSuite) TestPushPull(c *C) {

// Pull and check that we still get i
buf := bytes.NewBuffer(nil)
err = Pull(ctx, buf, s.profile, p.ArtifactPath)
err = Pull(ctx, buf, s.profile, p.ArtifactFile)
c.Assert(err, IsNil)
str, err := ioutil.ReadAll(buf)
c.Assert(err, IsNil)
Expand Down
5 changes: 3 additions & 2 deletions pkg/kando/chronicle_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func newChroniclePushCommand() *cobra.Command {
}
cmd.PersistentFlags().StringVarP(&params.ProfilePath, profilePathFlagName, "p", "", "Path to a Profile as a JSON string (required)")
cmd.MarkPersistentFlagRequired(profilePathFlagName)
cmd.PersistentFlags().StringVarP(&params.ArtifactPath, artifactPathFlagName, "s", "", "Specify a path suffix (optional)")
cmd.PersistentFlags().StringVarP(&params.EnvDir, envDirFlagName, "e", "", "Get environment variables from a (optional)")
cmd.PersistentFlags().StringVarP(&params.ArtifactFile, artifactPathFlagName, "s", "", "Specify a file that contains an object store suffix")
cmd.MarkPersistentFlagRequired(artifactPathFlagName)
cmd.PersistentFlags().StringVarP(&params.EnvDir, envDirFlagName, "e", "", "Get environment variables from a envdir style directory(optional)")
cmd.PersistentFlags().DurationVarP(&params.Frequency, frequencyFlagName, "f", time.Minute, "The Frequency to push to object storage ")
return cmd
}

0 comments on commit 809a5d0

Please sign in to comment.