Skip to content

Commit

Permalink
Resolve relative paths to absolute paths in command line arguments
Browse files Browse the repository at this point in the history
The executor accepts a few arguments dockerfile, context, cache-dir and
digest-file that all represent paths. This commits allows those paths to
be relative to the working directory of the executor.

Fixes GoogleContainerTools#732 GoogleContainerTools#31 GoogleContainerTools#675
  • Loading branch information
huguesalary committed Aug 9, 2019
1 parent 56eeaf4 commit f8427fb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ var RootCmd = &cobra.Command{
if err := executor.CheckPushPermissions(opts); err != nil {
exit(errors.Wrap(err, "error checking push permissions -- make sure you entered the correct tag name, and that you are authenticated correctly, and try again"))
}
if err := resolveRelativePaths(); err != nil {
exit(errors.Wrap(err, "error resolving relative paths to absolute paths"))
}
if err := os.Chdir("/"); err != nil {
exit(errors.Wrap(err, "error changing to root dir"))
}
Expand Down Expand Up @@ -109,6 +112,39 @@ var RootCmd = &cobra.Command{
},
}

func resolveRelativePaths() error {
optsPaths := []*string{
&opts.DockerfilePath,
&opts.SrcContext,
&opts.CacheDir,
&opts.TarPath,
&opts.DigestFile,
}

for _, p := range optsPaths {
// Skip empty path
if *p == "" {
continue
}
// Skip path that is already absolute
if filepath.IsAbs(*p) {
logrus.Debugf("Path %s is absolute, skipping", *p)
continue
}

// Resolve relative path to absolute path
var err error
var absp string
absp = *p
if *p, err = filepath.Abs(*p); err != nil {
return errors.Wrapf(err, "Couldn't resolve relative path %s to an absolute path", p)
}
logrus.Debugf("Resolved relative path %s to %s", *p, absp)
}

return nil
}

// addKanikoOptionsFlags configures opts
func addKanikoOptionsFlags(cmd *cobra.Command) {
RootCmd.PersistentFlags().StringVarP(&opts.DockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
Expand Down

0 comments on commit f8427fb

Please sign in to comment.