-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -17,6 +20,8 @@ | |
version = "0.3.2-pre"; | ||
# LINT.ThenChange(main.go:version) | ||
in { | ||
# Formatter to be used by helped func | ||
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. Please remove this comment. |
||
|
||
# Nix package | ||
packages = forAllSystems (system: | ||
let pkgs = pkgsFor system; in { | ||
|
@@ -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}; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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. A better error message:
Also, |
||
} | ||
|
||
cmd := exec.Command(nixfmtPath) | ||
cmd := exec.Command(formatterPath) | ||
cmd.Stdin = bytes.NewBuffer(contents) | ||
|
||
// Overwrite contents with formatted output. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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).`) | ||
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. Can we keep the
|
||
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).`) | ||
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. How about:
|
||
var version = flag.Bool("version", false, "display version and exit") | ||
|
||
type OsGetWd struct{} | ||
|
@@ -51,7 +51,7 @@ func (*OsGetWd) GetWd() (string, error) { | |
|
||
func main() { | ||
flag.Parse() | ||
|
||
fmt.Println(*formatter) | ||
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. Please remove. |
||
if *version { | ||
fmt.Printf("compose2nix v%s\n", appVersion) | ||
return | ||
|
@@ -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{ | ||
|
@@ -112,7 +115,8 @@ func main() { | |
NoCreateRootTarget: !*createRootTarget, | ||
WriteHeader: true, | ||
NoWriteNixSetup: !*writeNixSetup, | ||
AutoFormat: *autoFormat, | ||
AutoFormat: *formatter != "", | ||
Formatter: *formatter, | ||
DefaultStopTimeout: *defaultStopTimeout, | ||
IncludeBuild: *build, | ||
GetWorkingDir: &OsGetWd{}, | ||
|
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.
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: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.
👍