-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CDConfig: Add cd_content for file templating #61
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
|
@@ -22,16 +23,17 @@ type StepCreateCD struct { | |
// Files can be either files or directories. Any files provided here will | ||
// be written to the root of the CD. Directories will be written to the | ||
// root of the CD as well, but will retain their subdirectory structure. | ||
Files []string | ||
Label string | ||
Files []string | ||
Content map[string]string | ||
Label string | ||
|
||
CDPath string | ||
|
||
rootFolder string | ||
} | ||
|
||
func (s *StepCreateCD) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
if len(s.Files) == 0 { | ||
if len(s.Files) == 0 && len(s.Content) == 0 { | ||
log.Println("No CD files specified. CD disk will not be made.") | ||
return multistep.ActionContinue | ||
} | ||
|
@@ -79,6 +81,15 @@ func (s *StepCreateCD) Run(ctx context.Context, state multistep.StateBag) multis | |
} | ||
} | ||
|
||
for path, content := range s.Content { | ||
err = s.AddContent(rootFolder, path, content) | ||
if err != nil { | ||
state.Put("error", | ||
fmt.Errorf("Error creating temporary file for CD: %s", err)) | ||
return multistep.ActionHalt | ||
} | ||
} | ||
|
||
cmd, err := retrieveCDISOCreationCommand(s.Label, rootFolder, CDPath) | ||
if err != nil { | ||
state.Put("error", err) | ||
|
@@ -291,3 +302,18 @@ func (s *StepCreateCD) AddFile(dst, src string) error { | |
|
||
return filepath.Walk(src, visit) | ||
} | ||
|
||
func (s *StepCreateCD) AddContent(dst, path, content string) error { | ||
// Join Cleans the path so we can join it without path traversal issues. | ||
dstPath := filepath.Join(dst, path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @azr From testing, doing |
||
dstDir := filepath.Dir(dstPath) | ||
err := os.MkdirAll(dstDir, 0777) | ||
if err != nil { | ||
return fmt.Errorf("error creating new directory %s: %s", dstDir, err) | ||
} | ||
err = ioutil.WriteFile(dstPath, []byte(content), 0666) | ||
if err != nil { | ||
return fmt.Errorf("Error writing file %s on CD: %s", path, err) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this 👍🏼