Skip to content

Commit

Permalink
fix: improve generate command
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Mar 19, 2024
1 parent 21805ca commit 951c12b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/generate_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var generateSystemdCmd = &cobra.Command{
}
err = template.GenerateTemplate(systemd, outputFile)
if err != nil {
fmt.Println(err)
fmt.Printf("error generating templated file: %v\n", err)
os.Exit(1)
}
},
Expand All @@ -52,6 +52,6 @@ func init() {
generateCmd.AddCommand(generateSystemdCmd)

generateSystemdCmd.Flags().StringVarP(&installationPath, "installation-path", "p", "/usr/local/bin", "fwdctl installation path")
generateSystemdCmd.Flags().StringVarP(&c.RulesFile, "file", "f", "rules.yml", "rules file")
generateSystemdCmd.Flags().StringVarP(&c.RulesFile, "file", "f", "rules.yml", "rules file path")
generateSystemdCmd.Flags().StringVarP(&serviceType, "type", "t", "oneshot", "systemd service type [oneshot, fork]")
}
18 changes: 14 additions & 4 deletions pkg/template/systemd_template/systemd_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package systemd_template

import (
_ "embed"
"errors"
"fmt"
"os"
"path/filepath"
)

Expand All @@ -28,15 +30,23 @@ func serviceTypeAllowed(st string) bool {
}

func NewSystemdService(serviceType, installationPath, rulesFile string) (*SystemdService, error) {
// checks for systemd service type
if !serviceTypeAllowed(serviceType) {
return nil, fmt.Errorf("service type provided not allowed")
return nil, fmt.Errorf("service type is not allowed: %s", serviceType)
}
// checks for installation path
if !filepath.IsAbs(installationPath) {
// TODO: substitute with fmt.Errorf()
return nil, fmt.Errorf("installation path is not absolute")
return nil, fmt.Errorf("installation path is not absolute: %s", installationPath)
}
if _, err := os.Stat(installationPath); errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("installation path does not exist: %s", installationPath)
}
// checks for rules file
if !filepath.IsAbs(rulesFile) {
return nil, fmt.Errorf("rules file is not absolute")
return nil, fmt.Errorf("rules file path is not absolute: %s", rulesFile)
}
if _, err := os.Stat(installationPath); errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("rules file path does not exist: %s", rulesFile)
}

return &SystemdService{
Expand Down
22 changes: 13 additions & 9 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
)

Expand All @@ -14,27 +15,30 @@ type Generator interface {
GetFileName() string
}

func GenerateTemplate(g Generator, destinationPath string) error {
func GenerateTemplate(g Generator, outputPath string) error {
tpl, err := template.New(g.GetTemplateName()).Parse(g.GetFileContent())
if err != nil {
return err
return fmt.Errorf("error getting template instance: %v", err)
}

if destinationPath == "" {
return nil
if !filepath.IsAbs(outputPath) {
return fmt.Errorf("output path is not absolute: %s", outputPath)
}
if !filepath.IsAbs(destinationPath){
return fmt.Errorf("destinationPath path is not absolute")
// if last char of outputPath is "/" we want to remove,
// so the final output will be cleaned.
// this way: /root/template.file instead of /root//template.file
if outputPath != "/" && outputPath[len(outputPath)-1:] == "/" {
outputPath = strings.TrimSuffix(outputPath, "/")
}

outFile, err := os.Create(destinationPath + "/" + g.GetFileName())
outFile, err := os.Create(filepath.Join(outputPath, g.GetFileName()))
if err != nil {
return err
return fmt.Errorf("error creating output file: %v", err)
}

err = tpl.Execute(outFile, g.GetTemplateStruct())
if err != nil {
return err
return fmt.Errorf("error writing content into file: %v", err)
}
return nil
}

0 comments on commit 951c12b

Please sign in to comment.