checkpoint: remove private notes

- Implemented removing of private notes
- build: build enbas without rebuilding all packages by default to speed
  up local builds for development
- added new error types for unsupported add and remove operations
This commit is contained in:
Dan Anglin 2024-05-22 10:42:46 +01:00
parent e6fdb621aa
commit 891f34041f
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
5 changed files with 103 additions and 5 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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"
}

View file

@ -41,6 +41,7 @@ func (c *removeCommand) Execute() error {
funcMap := map[string]func(*client.Client) error{
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
}

View file

@ -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.