diff --git a/README.asciidoc b/README.asciidoc index 6fd09f6..84f9f67 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -63,6 +63,23 @@ ENBAS_INSTALL_PREFIX=${HOME}/.local mage install This will install Enbas to `~/.local/bin/enbas`. +===== Environment variables you can use with Mage + +[%header,cols=2*] +|=== +|Environment Variable +|Description + +|`ENBAS_INSTALL_PREFIX` +|Set this to your preferred the installation prefix (default: `/usr/local`). + +|`ENBAS_BUILD_REBUILD_ALL` +|Set this to "1" to rebuild all packages even if they are already up-to-date. + +|`ENBAS_BUILD_VERBOSE` +|Set this to "1" to enable verbose logging when building the binary. +|=== + ==== Install with go If your `GOBIN` directory is included in your `PATH` then you can install Enbas with Go. diff --git a/cmd/enbas/add.go b/cmd/enbas/add.go index 93633f9..b860b75 100644 --- a/cmd/enbas/add.go +++ b/cmd/enbas/add.go @@ -67,7 +67,10 @@ func (c *addCommand) addToList(gtsClient *client.Client) error { doFunc, ok := funcMap[c.resourceType] if !ok { - return unsupportedResourceTypeError{resourceType: c.resourceType} + return unsupportedAddOperationError{ + ResourceType: c.resourceType, + AddToResourceType: c.toResourceType, + } } return doFunc(gtsClient) @@ -109,7 +112,10 @@ func (c *addCommand) addToAccount(gtsClient *client.Client) error { doFunc, ok := funcMap[c.resourceType] if !ok { - return unsupportedResourceTypeError{resourceType: c.resourceType} + return unsupportedAddOperationError{ + ResourceType: c.resourceType, + AddToResourceType: c.toResourceType, + } } return doFunc(gtsClient) diff --git a/cmd/enbas/errors.go b/cmd/enbas/errors.go index e2be4f9..670113d 100644 --- a/cmd/enbas/errors.go +++ b/cmd/enbas/errors.go @@ -37,3 +37,21 @@ type noAccountSpecifiedError struct{} func (e noAccountSpecifiedError) Error() string { return "no account specified in this request" } + +type unsupportedAddOperationError struct { + ResourceType string + AddToResourceType string +} + +func (e unsupportedAddOperationError) Error() string { + return "adding '" + e.ResourceType + "' to '" + e.AddToResourceType + "' is not supported" +} + +type unsupportedRemoveOperationError struct { + ResourceType string + RemoveFromResourceType string +} + +func (e unsupportedRemoveOperationError) Error() string { + return "removing '" + e.ResourceType + "' from '" + e.RemoveFromResourceType + "' is not supported" +} diff --git a/cmd/enbas/remove.go b/cmd/enbas/remove.go index e5ebbde..f6e4c4a 100644 --- a/cmd/enbas/remove.go +++ b/cmd/enbas/remove.go @@ -40,7 +40,8 @@ func (c *removeCommand) Execute() error { } funcMap := map[string]func(*client.Client) error{ - listResource: c.removeFromList, + listResource: c.removeFromList, + accountResource: c.removeFromAccount, } doFunc, ok := funcMap[c.fromResourceType] @@ -63,7 +64,10 @@ func (c *removeCommand) removeFromList(gtsClient *client.Client) error { doFunc, ok := funcMap[c.resourceType] if !ok { - return unsupportedResourceTypeError{resourceType: c.resourceType} + return unsupportedRemoveOperationError{ + ResourceType: c.resourceType, + RemoveFromResourceType: c.fromResourceType, + } } return doFunc(gtsClient) @@ -97,3 +101,38 @@ func (c *removeCommand) removeAccountsFromList(gtsClient *client.Client) error { return nil } + +func (c *removeCommand) removeFromAccount(gtsClient *client.Client) error { + funcMap := map[string]func(*client.Client) error{ + noteResource: c.removeNoteFromAccount, + } + + doFunc, ok := funcMap[c.resourceType] + if !ok { + return unsupportedRemoveOperationError{ + ResourceType: c.resourceType, + RemoveFromResourceType: c.fromResourceType, + } + } + + return doFunc(gtsClient) +} + +func (c *removeCommand) removeNoteFromAccount(gtsClient *client.Client) error { + if len(c.accountNames) != 1 { + return fmt.Errorf("unexpected number of accounts specified; want 1, got %d", len(c.accountNames)) + } + + accountID, err := getAccountID(gtsClient, false, c.accountNames[0]) + if err != nil { + return fmt.Errorf("received an error while getting the account ID; %w", err) + } + + if err := gtsClient.SetPrivateNote(accountID, ""); err != nil { + return fmt.Errorf("unable to remove the private note from the account; %w", err) + } + + fmt.Println("Successfully removed the private note from the account.") + + return nil +} diff --git a/internal/build/magefiles/mage.go b/internal/build/magefiles/mage.go index af17039..3271229 100644 --- a/internal/build/magefiles/mage.go +++ b/internal/build/magefiles/mage.go @@ -19,6 +19,8 @@ const ( envInstallPrefix = "ENBAS_INSTALL_PREFIX" envTestVerbose = "ENBAS_TEST_VERBOSE" envTestCover = "ENBAS_TEST_COVER" + envBuildRebuildAll = "ENBAS_BUILD_REBUILD_ALL" + envBuildVerbose = "ENBAS_BUILD_VERBOSE" ) var Default = Build @@ -56,13 +58,29 @@ func Lint() error { } // Build build the executable. +// To rebuild packages that are already up-to-date set ENBAS_BUILD_REBUILD_ALL=1 +// To enable verbose mode set ENBAS_BUILD_VERBOSE=1 func Build() error { if err := changeToProjectRoot(); err != nil { return fmt.Errorf("unable to change to the project's root directory; %w", err) } + main := "./cmd/" + binary flags := ldflags() - return sh.Run("go", "build", "-ldflags="+flags, "-a", "-o", binary, "./cmd/enbas") + build := sh.RunCmd("go", "build") + args := []string{"-ldflags=" + flags, "-o", binary} + + if os.Getenv(envBuildRebuildAll) == "1" { + args = append(args, "-a") + } + + if os.Getenv(envBuildVerbose) == "1" { + args = append(args, "-v") + } + + args = append(args, main) + + return build(args...) } // Install install the executable.