Skip to content

Commit

Permalink
Adds .crashd directory when running the program
Browse files Browse the repository at this point in the history
This patch also adds a default value to the --args-file flag in the run
command. When no --args-file is used, the run command looks for a file
named args present in the $HOME/.crashd/ directory

Signed-off-by: Sagar Muchhal <muchhals@vmware.com>
  • Loading branch information
srm09 committed Sep 10, 2020
1 parent bc18d64 commit 689a4fe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/crash-dianostics.go
Expand Up @@ -56,10 +56,10 @@ func preRun(flags *globalFlags) error {
}
logrus.SetLevel(level)

return nil
return CreateCrashdDir()
}

// Run satarts the command
// Run starts the command
func Run() error {
logrus.SetOutput(os.Stdout)
return crashDiagnosticsCommand().Execute()
Expand Down
22 changes: 22 additions & 0 deletions cmd/defaults.go
@@ -0,0 +1,22 @@
package cmd

import (
"os"
"path/filepath"
)

var (
// Directory path created at crashd runtime
CrashdDir = filepath.Join(os.Getenv("HOME"), ".crashd")
// file path of the defaults args file
ArgsFile = filepath.Join(CrashdDir, "args")
)

// This creates a crashd directory which can be used as a default workdir
// for script execution. It will also house the default args file.
func CreateCrashdDir() error {
if _, err := os.Stat(CrashdDir); os.IsNotExist(err) {
return os.Mkdir(CrashdDir, 0755)
}
return nil
}
13 changes: 9 additions & 4 deletions cmd/run.go
Expand Up @@ -17,11 +17,16 @@ type runFlags struct {
argsFile string
}

func defaultRunFlags() *runFlags {
return &runFlags{
args: make(map[string]string),
argsFile: ArgsFile,
}
}

// newRunCommand creates a command to run the Diagnostics script a file
func newRunCommand() *cobra.Command {
flags := &runFlags{
args: make(map[string]string),
}
flags := defaultRunFlags()

cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Expand Down Expand Up @@ -62,7 +67,7 @@ func run(flags *runFlags, path string) error {
func processScriptArguments(flags *runFlags) (map[string]string, error) {
// read inputs from the scriptArgs-file
scriptArgs, err := util.ReadArgsFile(flags.argsFile)
if err != nil {
if err != nil && flags.argsFile != ArgsFile {
return nil, errors.Wrapf(err, "failed to parse scriptArgs file: %s", flags.argsFile)
}

Expand Down
23 changes: 23 additions & 0 deletions cmd/run_test.go
Expand Up @@ -4,8 +4,10 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
Expand All @@ -16,6 +18,21 @@ var _ = Describe("Run", func() {

Context("With args-file and args both", func() {

var argsBackupFile string

JustBeforeEach(func() {
if _, err := os.Stat(ArgsFile); err == nil {
argsBackupFile = fmt.Sprintf("%s.BKP.%s", ArgsFile, time.Now().String())
Expect(os.Rename(ArgsFile, argsBackupFile)).NotTo(HaveOccurred())
}
})

JustAfterEach(func() {
if argsBackupFile != "" {
Expect(os.Rename(argsBackupFile, ArgsFile)).NotTo(HaveOccurred())
}
})

DescribeTable("processScriptArguments", func(argsFileContent string, args map[string]string, size int) {
f, err := ioutil.TempFile(os.TempDir(), "")
Expect(err).NotTo(HaveOccurred())
Expand All @@ -38,5 +55,11 @@ var _ = Describe("Run", func() {
Entry("file with no keys", "", map[string]string{"key": "b"}, 1),
Entry("with file and without args", "key=value", map[string]string{}, 1),
)

It("no args file and args", func() {
scriptArgs, err := processScriptArguments(defaultRunFlags())
Expect(err).NotTo(HaveOccurred())
Expect(scriptArgs).To(HaveLen(0))
})
})
})

0 comments on commit 689a4fe

Please sign in to comment.