Skip to content
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

Add support for formatter specification #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

@aksiksi aksiksi Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format/line this up with the other struct fields.

Note for next time: You can either set this up in your editor, or use go fmt to format all files:

go fmt ./...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
79 changes: 79 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
onchg.url = "github:aksiksi/onchg-rs";
onchg.inputs.nixpkgs.follows = "nixpkgs";

alejandra.url = "github:kamadorueda/alejandra/3.1.0";
alejandra.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, onchg, ... }: let
outputs = { self, nixpkgs, onchg, alejandra, ... }: let
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
pkgsFor = system: nixpkgs.legacyPackages.${system};
Expand All @@ -17,6 +20,8 @@
version = "0.3.2-pre";
# LINT.ThenChange(main.go:version)
in {
# Formatter to be used by helped func
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this comment.


# Nix package
packages = forAllSystems (system:
let pkgs = pkgsFor system; in {
Expand All @@ -33,7 +38,7 @@
devShells = forAllSystems (system:
let pkgs = pkgsFor system; in {
default = pkgs.mkShell {
buildInputs = [ pkgs.go pkgs.gopls pkgs.nixfmt-rfc-style ];
buildInputs = [ pkgs.go pkgs.gopls pkgs.nixfmt-rfc-style alejandra.defaultPackage.${system} ];
# Add a Git pre-commit hook.
shellHook = onchg.shellHook.${system};
};
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 @@

// 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better error message:

fmt.Errorf("configured formatter %q not found in $PATH: %w", formatter, err)

%q will wrap the string in double quotes (").

Also, %w is only used to wrap errors in Go.

}

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).`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the auto_format flag? But change the help text to:

`if true, Nix output will be formatted using the formatter passed to the "formatter" flag (default: "nixfmt")`

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).`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

flag.String("formatter", "nixfmt", `if specified, Nix output will be formatted by the Nix formatter specified (must be present in $PATH). supported options are: "nixfmt" and "alejandra"`)

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

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
Loading