Skip to content

Commit

Permalink
Merge pull request #213 from DBCDK/gofmt
Browse files Browse the repository at this point in the history
enable gofmt, format code, fix `nix-command` by adding mainProgram
  • Loading branch information
srhb committed Mar 13, 2024
2 parents be415d3 + 3af4d2f commit 31a4824
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@

# treewide nix fmt after flakification
4a3684b18388d727f36a72f736f68ef379dbb209

# treewide nix fmt with gofmt enabled
7a77a23891d599c21a07ebe31a490d00fb835559
2 changes: 1 addition & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func FilterHostsTags(allHosts []nix.Host, selectedTags []string) (hosts []nix.Ho
include := true
for _, selectTag := range selectedTags {
if !hasTag(host, selectTag) {
include = false;
include = false
break
}

Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
meta = {
homepage = "https://github.com/DBCDK/morph";
description = "Morph is a NixOS host manager written in Golang.";
mainProgram = "morph";
};
};
};
Expand Down
78 changes: 37 additions & 41 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ type Deployment struct {
}

type NixContext struct {
EvalCmd string
BuildCmd string
ShellCmd string
EvalCmd string
BuildCmd string
ShellCmd string
EvalMachines string
ShowTrace bool
KeepGCRoot bool
AllowBuildShell bool
}

type NixBuildInvocationArgs struct {
ArgsFile string
Attr string
DeploymentPath string
Names []string
NixArgs []string
ArgsFile string
Attr string
DeploymentPath string
Names []string
NixArgs []string
NixBuildTargets string
NixConfig map[string]string
NixContext NixContext
ResultLinkPath string
NixConfig map[string]string
NixContext NixContext
ResultLinkPath string
}

func (nArgs *NixBuildInvocationArgs) ToNixBuildArgs() []string {
Expand Down Expand Up @@ -125,13 +125,13 @@ func (nArgs *NixEvalInvocationArgs) ToNixInstantiateArgs() []string {
}

type NixEvalInvocationArgs struct {
AsJSON bool
ArgsFile string
Attr string
AsJSON bool
ArgsFile string
Attr string
DeploymentPath string
NixContext NixContext
Strict bool
ReadWriteMode bool
NixContext NixContext
Strict bool
ReadWriteMode bool
}

func (host *Host) GetName() string {
Expand Down Expand Up @@ -221,19 +221,18 @@ func (host *Host) Reboot(sshContext *ssh.SSHContext) error {
func (ctx *NixContext) GetBuildShell(deploymentPath string) (buildShell *string, err error) {

nixEvalInvocationArgs := NixEvalInvocationArgs{
AsJSON: true,
Attr: "info.buildShell",
AsJSON: true,
Attr: "info.buildShell",
DeploymentPath: deploymentPath,
NixContext: *ctx,
Strict: true,
NixContext: *ctx,
Strict: true,
}

jsonArgs, err := json.Marshal(nixEvalInvocationArgs)
if err != nil {
return buildShell, err
}


cmd := exec.Command(ctx.EvalCmd, nixEvalInvocationArgs.ToNixInstantiateArgs()...)

var stdout bytes.Buffer
Expand Down Expand Up @@ -266,13 +265,12 @@ func (ctx *NixContext) GetBuildShell(deploymentPath string) (buildShell *string,
func (ctx *NixContext) EvalHosts(deploymentPath string, attr string) (string, error) {
attribute := "nodes." + attr


nixEvalInvocationArgs := NixEvalInvocationArgs{
AsJSON: false,
Attr: attribute,
AsJSON: false,
Attr: attribute,
DeploymentPath: deploymentPath,
NixContext: *ctx,
Strict: true,
NixContext: *ctx,
Strict: true,
}

jsonArgs, err := json.Marshal(nixEvalInvocationArgs)
Expand All @@ -299,19 +297,18 @@ func (ctx *NixContext) EvalHosts(deploymentPath string, attr string) (string, er
func (ctx *NixContext) GetMachines(deploymentPath string) (deployment Deployment, err error) {

nixEvalInvocationArgs := NixEvalInvocationArgs{
AsJSON: true,
Attr: "info.deployment",
AsJSON: true,
Attr: "info.deployment",
DeploymentPath: deploymentPath,
NixContext: *ctx,
Strict: true,
NixContext: *ctx,
Strict: true,
}

jsonArgs, err := json.Marshal(nixEvalInvocationArgs)
if err != nil {
return deployment, err
}


cmd := exec.Command(ctx.EvalCmd, nixEvalInvocationArgs.ToNixInstantiateArgs()...)

var stdout bytes.Buffer
Expand Down Expand Up @@ -378,15 +375,15 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg

argsFile := tmpdir + "/morph-args.json"
NixBuildInvocationArgs := NixBuildInvocationArgs{
ArgsFile: argsFile,
Attr: "machines",
DeploymentPath: deploymentPath,
Names: hostNames,
NixArgs: nixArgs,
ArgsFile: argsFile,
Attr: "machines",
DeploymentPath: deploymentPath,
Names: hostNames,
NixArgs: nixArgs,
NixBuildTargets: nixBuildTargets,
NixConfig: hosts[0].NixConfig,
NixContext: *ctx,
ResultLinkPath: resultLinkPath,
NixConfig: hosts[0].NixConfig,
NixContext: *ctx,
ResultLinkPath: resultLinkPath,
}

jsonArgs, err := json.Marshal(NixBuildInvocationArgs)
Expand All @@ -399,7 +396,6 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg
return "", err
}


var cmd *exec.Cmd
if ctx.AllowBuildShell && buildShell != nil {

Expand Down
1 change: 1 addition & 0 deletions treefmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
shellcheck.enable = true; # bash/shell
taplo.enable = true; # toml
yamlfmt.enable = true; # yaml
gofmt.enable = true;
};
settings = {
formatter = {
Expand Down
2 changes: 1 addition & 1 deletion utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Create a context with a timeout, but only if the timeout is longer than 0
func ContextWithConditionalTimeout(parent context.Context, timeout int) (context.Context, context.CancelFunc) {
var (
ctx context.Context
ctx context.Context
cancel context.CancelFunc
)

Expand Down
4 changes: 2 additions & 2 deletions utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetAbsPathRelativeTo(path string, reference string) string {
}
}

func ValidateEnvironment(dependencies ...string) {
func ValidateEnvironment(dependencies ...string) {
missingDepencies := make([]string, 0)
for _, dependency := range dependencies {
_, err := exec.LookPath(dependency)
Expand All @@ -27,7 +27,7 @@ func ValidateEnvironment(dependencies ...string) {
}

if len(missingDepencies) > 0 {
fmt.Fprint(os.Stderr, errors.New("Missing dependencies: '" + strings.Join(missingDepencies, ", ") + "' on $PATH"))
fmt.Fprint(os.Stderr, errors.New("Missing dependencies: '"+strings.Join(missingDepencies, ", ")+"' on $PATH"))
Exit(1)
}
}
6 changes: 3 additions & 3 deletions utils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type FinalizerFunc func()
var finalizers []*finalizer

/*
Finalizers run sequentially at morph shutdown - both at clean shutdown and on errors.
Finalizers should be quick, simple and they need to ignore errors and don't panic.
Each finalizer will only run once and will _never_ be re-invoked.
Finalizers run sequentially at morph shutdown - both at clean shutdown and on errors.
Finalizers should be quick, simple and they need to ignore errors and don't panic.
Each finalizer will only run once and will _never_ be re-invoked.
*/
func (f *finalizer) Run() {
if !f.executed {
Expand Down

0 comments on commit 31a4824

Please sign in to comment.