Skip to content

Commit

Permalink
Add go files
Browse files Browse the repository at this point in the history
  • Loading branch information
FBIGlowie committed Nov 27, 2024
1 parent 21959fe commit c942929
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type Generator struct {
RemoveVolumes bool
NoCreateRootTarget bool
AutoFormat bool
Formatter string
WriteHeader bool
NoWriteNixSetup bool
DefaultStopTimeout time.Duration
Expand Down Expand Up @@ -210,6 +211,7 @@ func (g *Generator) Run(ctx context.Context) (*NixContainerConfig, error) {
Networks: networks,
Volumes: volumes,
CreateRootTarget: !g.NoCreateRootTarget,
Formatter: g.Formatter,
AutoStart: g.AutoStart,
WriteNixSetup: !g.NoWriteNixSetup,
AutoFormat: g.AutoFormat,
Expand Down
9 changes: 5 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ func ReadEnvFiles(envFiles []string, mergeWithEnv, ignoreMissing bool) (env []st

// formatNixCode will format Nix code by calling 'nixfmt' and passing in the
// given code via stdin.
func formatNixCode(contents []byte) ([]byte, error) {
func formatNixCode(contents []byte, formatter string) ([]byte, error) {
//
// Check for existence of 'nixfmt' in $PATH.
nixfmtPath, err := exec.LookPath("nixfmt")
formatterPath, err := exec.LookPath(formatter)
if err != nil {
return nil, fmt.Errorf("'nixfmt' not found in $PATH: %w", err)
return nil, fmt.Errorf("%w not found in $PATH: %w", formatter, err)

Check failure on line 84 in helpers.go

View workflow job for this annotation

GitHub Actions / linux-test

fmt.Errorf format %w has arg formatter of wrong type string
}

cmd := exec.Command(nixfmtPath)
cmd := exec.Command(formatterPath)
cmd.Stdin = bytes.NewBuffer(contents)

// Overwrite contents with formatted output.
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var createRootTarget = flag.Bool("create_root_target", true, "if set, a root sys
var defaultStopTimeout = flag.Duration("default_stop_timeout", defaultSystemdStopTimeout, "default stop timeout for generated container services.")
var build = flag.Bool("build", false, "if set, generated container build systemd services will be enabled.")
var writeNixSetup = flag.Bool("write_nix_setup", true, "if true, Nix setup code is written to output (runtime, DNS, autoprune, etc.)")
var autoFormat = flag.Bool("auto_format", false, `if true, Nix output will be formatted using "nixfmt" (must be present in $PATH).`)
var formatter = flag.String("formatter", "", `if specified, Nix output will be formatted by formatter specified, supported are nixfmt and alejandra (must be present in $PATH).`)
var version = flag.Bool("version", false, "display version and exit")

type OsGetWd struct{}
Expand All @@ -51,7 +51,7 @@ func (*OsGetWd) GetWd() (string, error) {

func main() {
flag.Parse()

fmt.Println(*formatter)
if *version {
fmt.Printf("compose2nix v%s\n", appVersion)
return
Expand Down Expand Up @@ -90,6 +90,9 @@ func main() {
}
serviceIncludeRegexp = pat
}
if *formatter != "" && (strings.TrimSpace(*formatter) != "nixfmt" && strings.TrimSpace(*formatter) != "alejandra") {
log.Fatal("Invalid formatter provided, needs to be either nixfmt or alejandra")
}

start := time.Now()
g := Generator{
Expand All @@ -112,7 +115,8 @@ func main() {
NoCreateRootTarget: !*createRootTarget,
WriteHeader: true,
NoWriteNixSetup: !*writeNixSetup,
AutoFormat: *autoFormat,
AutoFormat: *formatter != "",
Formatter: *formatter,
DefaultStopTimeout: *defaultStopTimeout,
IncludeBuild: *build,
GetWorkingDir: &OsGetWd{},
Expand Down
3 changes: 2 additions & 1 deletion nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ type NixContainerConfig struct {
Volumes []*NixVolume
CreateRootTarget bool
WriteNixSetup bool
Formatter string
AutoFormat bool
AutoStart bool
IncludeBuild bool
Expand Down Expand Up @@ -310,7 +311,7 @@ func (c *NixContainerConfig) Write(out io.Writer) error {
config := []byte(c.String())

if c.AutoFormat {
formatted, err := formatNixCode(config)
formatted, err := formatNixCode(config, c.Formatter)
if err != nil {
return err
}
Expand Down

0 comments on commit c942929

Please sign in to comment.