Compare commits
No commits in common. "08df511506b5507303531241a964764cbc739410" and "ecc8d51c098d75d6be0c918875b5bbe84dd293c2" have entirely different histories.
08df511506
...
ecc8d51c09
18 changed files with 124 additions and 180 deletions
|
@ -8,7 +8,7 @@ import (
|
|||
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
||||
)
|
||||
|
||||
func getAccountID(gtsClient *client.Client, myAccount bool, accountName, configDir string) (string, error) {
|
||||
func getAccountID(gtsClient *client.Client, myAccount bool, accountName string) (string, error) {
|
||||
var (
|
||||
accountID string
|
||||
err error
|
||||
|
@ -16,7 +16,7 @@ func getAccountID(gtsClient *client.Client, myAccount bool, accountName, configD
|
|||
|
||||
switch {
|
||||
case myAccount:
|
||||
accountID, err = getMyAccountID(gtsClient, configDir)
|
||||
accountID, err = getMyAccountID(gtsClient)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get your account ID; %w", err)
|
||||
}
|
||||
|
@ -41,8 +41,8 @@ func getTheirAccountID(gtsClient *client.Client, accountURI string) (string, err
|
|||
return account.ID, nil
|
||||
}
|
||||
|
||||
func getMyAccountID(gtsClient *client.Client, configDir string) (string, error) {
|
||||
account, err := getMyAccount(gtsClient, configDir)
|
||||
func getMyAccountID(gtsClient *client.Client) (string, error) {
|
||||
account, err := getMyAccount(gtsClient)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("received an error while getting your account details; %w", err)
|
||||
}
|
||||
|
@ -50,8 +50,8 @@ func getMyAccountID(gtsClient *client.Client, configDir string) (string, error)
|
|||
return account.ID, nil
|
||||
}
|
||||
|
||||
func getMyAccount(gtsClient *client.Client, configDir string) (model.Account, error) {
|
||||
authConfig, err := config.NewCredentialsConfigFromFile(configDir)
|
||||
func getMyAccount(gtsClient *client.Client) (model.Account, error) {
|
||||
authConfig, err := config.NewAuthenticationConfigFromFile()
|
||||
if err != nil {
|
||||
return model.Account{}, fmt.Errorf("unable to retrieve the authentication configuration; %w", err)
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
type addCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
toResourceType string
|
||||
listID string
|
||||
|
@ -19,13 +18,12 @@ type addCommand struct {
|
|||
content string
|
||||
}
|
||||
|
||||
func newAddCommand(tlf topLevelFlags, name, summary string) *addCommand {
|
||||
func newAddCommand(name, summary string) *addCommand {
|
||||
emptyArr := make([]string, 0, 3)
|
||||
|
||||
command := addCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountNames: accountNames(emptyArr),
|
||||
topLevelFlags: tlf,
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountNames: accountNames(emptyArr),
|
||||
}
|
||||
|
||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the resource type to add (e.g. account, note)")
|
||||
|
@ -54,7 +52,7 @@ func (c *addCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.toResourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
@ -128,7 +126,7 @@ func (c *addCommand) addNoteToAccount(gtsClient *client.Client) error {
|
|||
return fmt.Errorf("unexpected number of accounts specified; want 1, got %d", len(c.accountNames))
|
||||
}
|
||||
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountNames[0], c.topLevelFlags.configDir)
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountNames[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
|
|
@ -10,17 +10,15 @@ import (
|
|||
type blockCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
accountName string
|
||||
unblock bool
|
||||
}
|
||||
|
||||
func newBlockCommand(tlf topLevelFlags, name, summary string, unblock bool) *blockCommand {
|
||||
func newBlockCommand(name, summary string, unblock bool) *blockCommand {
|
||||
command := blockCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
|
||||
topLevelFlags: tlf,
|
||||
unblock: unblock,
|
||||
}
|
||||
|
||||
|
@ -42,7 +40,7 @@ func (c *blockCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.resourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
@ -51,7 +49,7 @@ func (c *blockCommand) Execute() error {
|
|||
}
|
||||
|
||||
func (c *blockCommand) blockAccount(gtsClient *client.Client) error {
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountName, c.topLevelFlags.configDir)
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
|
|
@ -11,17 +11,14 @@ import (
|
|||
type createCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
listTitle string
|
||||
listRepliesPolicy string
|
||||
}
|
||||
|
||||
func newCreateCommand(tlf topLevelFlags, name, summary string) *createCommand {
|
||||
func newCreateCommand(name, summary string) *createCommand {
|
||||
command := createCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
|
||||
topLevelFlags: tlf,
|
||||
}
|
||||
|
||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to create")
|
||||
|
@ -38,7 +35,7 @@ func (c *createCommand) Execute() error {
|
|||
return flagNotSetError{flagText: resourceTypeFlag}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
|
|
@ -10,16 +10,13 @@ import (
|
|||
type deleteCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
listID string
|
||||
}
|
||||
|
||||
func newDeleteCommand(tlf topLevelFlags, name, summary string) *deleteCommand {
|
||||
func newDeleteCommand(name, summary string) *deleteCommand {
|
||||
command := deleteCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
|
||||
topLevelFlags: tlf,
|
||||
}
|
||||
|
||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to delete")
|
||||
|
@ -44,7 +41,7 @@ func (c *deleteCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.resourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,3 @@ func (a *accountNames) Set(value string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
type topLevelFlags struct {
|
||||
configDir string
|
||||
}
|
||||
|
|
|
@ -10,19 +10,18 @@ import (
|
|||
type followCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
accountName string
|
||||
showReposts bool
|
||||
notify bool
|
||||
unfollow bool
|
||||
resourceType string
|
||||
accountName string
|
||||
showReposts bool
|
||||
notify bool
|
||||
unfollow bool
|
||||
}
|
||||
|
||||
func newFollowCommand(tlf topLevelFlags, name, summary string, unfollow bool) *followCommand {
|
||||
func newFollowCommand(name, summary string, unfollow bool) *followCommand {
|
||||
command := followCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
unfollow: unfollow,
|
||||
topLevelFlags: tlf,
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
|
||||
unfollow: unfollow,
|
||||
}
|
||||
|
||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to follow")
|
||||
|
@ -45,7 +44,7 @@ func (c *followCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.resourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
@ -54,7 +53,7 @@ func (c *followCommand) Execute() error {
|
|||
}
|
||||
|
||||
func (c *followCommand) followAccount(gtsClient *client.Client) error {
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountName, c.topLevelFlags.configDir)
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
|
|
@ -12,15 +12,12 @@ import (
|
|||
|
||||
type loginCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
instance string
|
||||
}
|
||||
|
||||
func newLoginCommand(tlf topLevelFlags, name, summary string) *loginCommand {
|
||||
func newLoginCommand(name, summary string) *loginCommand {
|
||||
command := loginCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
topLevelFlags: tlf,
|
||||
instance: "",
|
||||
}
|
||||
|
||||
|
@ -91,7 +88,7 @@ Once you have the code please copy and paste it below.
|
|||
return fmt.Errorf("unable to verify the credentials; %w", err)
|
||||
}
|
||||
|
||||
loginName, err := config.SaveCredentials(c.topLevelFlags.configDir, account.Username, gtsClient.Authentication)
|
||||
loginName, err := config.SaveCredentials(account.Username, gtsClient.Authentication)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to save the authentication details; %w", err)
|
||||
}
|
||||
|
|
|
@ -88,10 +88,6 @@ func run() error {
|
|||
unblock: "unblock a resource (e.g. an account)",
|
||||
}
|
||||
|
||||
tlf := topLevelFlags{}
|
||||
|
||||
flag.StringVar(&tlf.configDir, "config-dir", "", "specify your config directory")
|
||||
|
||||
flag.Usage = enbasUsageFunc(summaries)
|
||||
|
||||
flag.Parse()
|
||||
|
@ -109,33 +105,33 @@ func run() error {
|
|||
|
||||
switch subcommand {
|
||||
case login:
|
||||
executor = newLoginCommand(tlf, login, summaries[login])
|
||||
executor = newLoginCommand(login, summaries[login])
|
||||
case version:
|
||||
executor = newVersionCommand(version, summaries[version])
|
||||
case showResource:
|
||||
executor = newShowCommand(tlf, showResource, summaries[showResource])
|
||||
executor = newShowCommand(showResource, summaries[showResource])
|
||||
case switchAccount:
|
||||
executor = newSwitchCommand(tlf, switchAccount, summaries[switchAccount])
|
||||
executor = newSwitchCommand(switchAccount, summaries[switchAccount])
|
||||
case createResource:
|
||||
executor = newCreateCommand(tlf, createResource, summaries[createResource])
|
||||
executor = newCreateCommand(createResource, summaries[createResource])
|
||||
case deleteResource:
|
||||
executor = newDeleteCommand(tlf, deleteResource, summaries[deleteResource])
|
||||
executor = newDeleteCommand(deleteResource, summaries[deleteResource])
|
||||
case updateResource:
|
||||
executor = newUpdateCommand(tlf, updateResource, summaries[updateResource])
|
||||
executor = newUpdateCommand(updateResource, summaries[updateResource])
|
||||
case whoami:
|
||||
executor = newWhoAmICommand(tlf, whoami, summaries[whoami])
|
||||
executor = newWhoAmICommand(whoami, summaries[whoami])
|
||||
case add:
|
||||
executor = newAddCommand(tlf, add, summaries[add])
|
||||
executor = newAddCommand(add, summaries[add])
|
||||
case remove:
|
||||
executor = newRemoveCommand(tlf, remove, summaries[remove])
|
||||
executor = newRemoveCommand(remove, summaries[remove])
|
||||
case follow:
|
||||
executor = newFollowCommand(tlf, follow, summaries[follow], false)
|
||||
executor = newFollowCommand(follow, summaries[follow], false)
|
||||
case unfollow:
|
||||
executor = newFollowCommand(tlf, unfollow, summaries[unfollow], true)
|
||||
executor = newFollowCommand(unfollow, summaries[unfollow], true)
|
||||
case block:
|
||||
executor = newBlockCommand(tlf, block, summaries[block], false)
|
||||
executor = newBlockCommand(block, summaries[block], false)
|
||||
case unblock:
|
||||
executor = newBlockCommand(tlf, unblock, summaries[unblock], true)
|
||||
executor = newBlockCommand(unblock, summaries[unblock], true)
|
||||
default:
|
||||
flag.Usage()
|
||||
|
||||
|
|
|
@ -10,20 +10,18 @@ import (
|
|||
type removeCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
fromResourceType string
|
||||
listID string
|
||||
accountNames accountNames
|
||||
}
|
||||
|
||||
func newRemoveCommand(tlf topLevelFlags, name, summary string) *removeCommand {
|
||||
func newRemoveCommand(name, summary string) *removeCommand {
|
||||
emptyArr := make([]string, 0, 3)
|
||||
|
||||
command := removeCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
accountNames: accountNames(emptyArr),
|
||||
topLevelFlags: tlf,
|
||||
}
|
||||
|
||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the resource type to remove (e.g. account, note)")
|
||||
|
@ -51,7 +49,7 @@ func (c *removeCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.fromResourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
@ -125,7 +123,7 @@ func (c *removeCommand) removeNoteFromAccount(gtsClient *client.Client) error {
|
|||
return fmt.Errorf("unexpected number of accounts specified; want 1, got %d", len(c.accountNames))
|
||||
}
|
||||
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountNames[0], c.topLevelFlags.configDir)
|
||||
accountID, err := getAccountID(gtsClient, false, c.accountNames[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
type showCommand struct {
|
||||
*flag.FlagSet
|
||||
topLevelFlags topLevelFlags
|
||||
myAccount bool
|
||||
showAccountRelationship bool
|
||||
showUserPreferences bool
|
||||
|
@ -24,10 +23,9 @@ type showCommand struct {
|
|||
limit int
|
||||
}
|
||||
|
||||
func newShowCommand(tlf topLevelFlags, name, summary string) *showCommand {
|
||||
func newShowCommand(name, summary string) *showCommand {
|
||||
command := showCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
topLevelFlags: tlf,
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
}
|
||||
|
||||
command.BoolVar(&command.myAccount, myAccountFlag, false, "set to true to lookup your account")
|
||||
|
@ -67,7 +65,7 @@ func (c *showCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.resourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
@ -93,7 +91,7 @@ func (c *showCommand) showAccount(gtsClient *client.Client) error {
|
|||
)
|
||||
|
||||
if c.myAccount {
|
||||
account, err = getMyAccount(gtsClient, c.topLevelFlags.configDir)
|
||||
account, err = getMyAccount(gtsClient)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account details; %w", err)
|
||||
}
|
||||
|
@ -236,7 +234,7 @@ func (c *showCommand) showLists(gtsClient *client.Client) error {
|
|||
}
|
||||
|
||||
func (c *showCommand) showFollowers(gtsClient *client.Client) error {
|
||||
accountID, err := getAccountID(gtsClient, c.myAccount, c.accountName, c.topLevelFlags.configDir)
|
||||
accountID, err := getAccountID(gtsClient, c.myAccount, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
@ -256,7 +254,7 @@ func (c *showCommand) showFollowers(gtsClient *client.Client) error {
|
|||
}
|
||||
|
||||
func (c *showCommand) showFollowing(gtsClient *client.Client) error {
|
||||
accountID, err := getAccountID(gtsClient, c.myAccount, c.accountName, c.topLevelFlags.configDir)
|
||||
accountID, err := getAccountID(gtsClient, c.myAccount, c.accountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
||||
}
|
||||
|
|
|
@ -9,15 +9,13 @@ import (
|
|||
|
||||
type switchCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
toAccount string
|
||||
}
|
||||
|
||||
func newSwitchCommand(tlf topLevelFlags, name, summary string) *switchCommand {
|
||||
func newSwitchCommand(name, summary string) *switchCommand {
|
||||
command := switchCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
topLevelFlags: tlf,
|
||||
toAccount: "",
|
||||
}
|
||||
|
||||
command.StringVar(&command.toAccount, toAccountFlag, "", "the account to switch to")
|
||||
|
@ -32,7 +30,7 @@ func (c *switchCommand) Execute() error {
|
|||
return flagNotSetError{flagText: toAccountFlag}
|
||||
}
|
||||
|
||||
if err := config.UpdateCurrentAccount(c.toAccount, c.topLevelFlags.configDir); err != nil {
|
||||
if err := config.UpdateCurrentAccount(c.toAccount); err != nil {
|
||||
return fmt.Errorf("unable to switch accounts; %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -11,17 +11,15 @@ import (
|
|||
type updateCommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
resourceType string
|
||||
listID string
|
||||
listTitle string
|
||||
listRepliesPolicy string
|
||||
}
|
||||
|
||||
func newUpdateCommand(tlf topLevelFlags, name, summary string) *updateCommand {
|
||||
func newUpdateCommand(name, summary string) *updateCommand {
|
||||
command := updateCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
topLevelFlags: tlf,
|
||||
}
|
||||
|
||||
command.StringVar(&command.resourceType, resourceTypeFlag, "", "specify the type of resource to update")
|
||||
|
@ -48,7 +46,7 @@ func (c *updateCommand) Execute() error {
|
|||
return unsupportedResourceTypeError{resourceType: c.resourceType}
|
||||
}
|
||||
|
||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.configDir)
|
||||
gtsClient, err := client.NewClientFromConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ func enbasUsageFunc(summaries map[string]string) func() {
|
|||
builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n")
|
||||
|
||||
if binaryVersion != "" {
|
||||
builder.WriteString("VERSION:\n " + binaryVersion + "\n\n")
|
||||
builder.WriteString("VERSION:\n " + binaryVersion + "\n\n")
|
||||
}
|
||||
|
||||
builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:")
|
||||
|
|
|
@ -9,14 +9,11 @@ import (
|
|||
|
||||
type whoAmICommand struct {
|
||||
*flag.FlagSet
|
||||
|
||||
topLevelFlags topLevelFlags
|
||||
}
|
||||
|
||||
func newWhoAmICommand(tlf topLevelFlags, name, summary string) *whoAmICommand {
|
||||
func newWhoAmICommand(name, summary string) *whoAmICommand {
|
||||
command := whoAmICommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
topLevelFlags: tlf,
|
||||
}
|
||||
|
||||
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||
|
@ -25,7 +22,7 @@ func newWhoAmICommand(tlf topLevelFlags, name, summary string) *whoAmICommand {
|
|||
}
|
||||
|
||||
func (c *whoAmICommand) Execute() error {
|
||||
config, err := config.NewCredentialsConfigFromFile(c.topLevelFlags.configDir)
|
||||
config, err := config.NewAuthenticationConfigFromFile()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load the credential config; %w", err)
|
||||
}
|
||||
|
|
|
@ -20,8 +20,8 @@ type Client struct {
|
|||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func NewClientFromConfig(configDir string) (*Client, error) {
|
||||
config, err := config.NewCredentialsConfigFromFile(configDir)
|
||||
func NewClientFromConfig() (*Client, error) {
|
||||
config, err := config.NewAuthenticationConfigFromFile()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get the authentication configuration; %w", err)
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func (g *Client) sendRequest(method string, url string, requestBody io.Reader, o
|
|||
request.Header.Set("User-Agent", g.UserAgent)
|
||||
|
||||
if len(g.Authentication.AccessToken) > 0 {
|
||||
request.Header.Set("Authorization", "Bearer "+g.Authentication.AccessToken)
|
||||
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", g.Authentication.AccessToken))
|
||||
}
|
||||
|
||||
response, err := g.HTTPClient.Do(request)
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -25,25 +27,14 @@ type Credentials struct {
|
|||
AccessToken string `json:"accessToken"`
|
||||
}
|
||||
|
||||
type CredentialsNotFoundError struct {
|
||||
AccountName string
|
||||
}
|
||||
|
||||
func (e CredentialsNotFoundError) Error() string {
|
||||
return "unable to find the credentials for the account '" + e.AccountName + "'"
|
||||
}
|
||||
|
||||
// SaveCredentials saves the credentials into the credentials file within the specified configuration
|
||||
// directory. If the directory is not specified then the default directory is used. If the directory
|
||||
// is not present, it will be created.
|
||||
func SaveCredentials(configDir, username string, credentials Credentials) (string, error) {
|
||||
if err := ensureConfigDir(calculateConfigDir(configDir)); err != nil {
|
||||
func SaveCredentials(username string, credentials Credentials) (string, error) {
|
||||
if err := ensureConfigDir(); err != nil {
|
||||
return "", fmt.Errorf("unable to ensure the configuration directory; %w", err)
|
||||
}
|
||||
|
||||
var authConfig CredentialsConfig
|
||||
|
||||
filepath := credentialsConfigFile(configDir)
|
||||
filepath := credentialsConfigFile()
|
||||
|
||||
if _, err := os.Stat(filepath); err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
|
@ -52,7 +43,7 @@ func SaveCredentials(configDir, username string, credentials Credentials) (strin
|
|||
|
||||
authConfig.Credentials = make(map[string]Credentials)
|
||||
} else {
|
||||
authConfig, err = NewCredentialsConfigFromFile(configDir)
|
||||
authConfig, err = NewAuthenticationConfigFromFile()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err)
|
||||
}
|
||||
|
@ -72,34 +63,15 @@ func SaveCredentials(configDir, username string, credentials Credentials) (strin
|
|||
|
||||
authConfig.Credentials[authenticationName] = credentials
|
||||
|
||||
if err := saveCredentialsConfigFile(authConfig, configDir); err != nil {
|
||||
if err := saveCredentialsConfigFile(authConfig); err != nil {
|
||||
return "", fmt.Errorf("unable to save the authentication configuration to file; %w", err)
|
||||
}
|
||||
|
||||
return authenticationName, nil
|
||||
}
|
||||
|
||||
func UpdateCurrentAccount(account string, configDir string) error {
|
||||
credentialsConfig, err := NewCredentialsConfigFromFile(configDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err)
|
||||
}
|
||||
|
||||
if _, ok := credentialsConfig.Credentials[account]; !ok {
|
||||
return CredentialsNotFoundError{account}
|
||||
}
|
||||
|
||||
credentialsConfig.CurrentAccount = account
|
||||
|
||||
if err := saveCredentialsConfigFile(credentialsConfig, configDir); err != nil {
|
||||
return fmt.Errorf("unable to save the authentication configuration to file; %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewCredentialsConfigFromFile(configDir string) (CredentialsConfig, error) {
|
||||
path := credentialsConfigFile(configDir)
|
||||
func NewAuthenticationConfigFromFile() (CredentialsConfig, error) {
|
||||
path := credentialsConfigFile()
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
|
@ -116,12 +88,58 @@ func NewCredentialsConfigFromFile(configDir string) (CredentialsConfig, error) {
|
|||
return authConfig, nil
|
||||
}
|
||||
|
||||
func saveCredentialsConfigFile(authConfig CredentialsConfig, configDir string) error {
|
||||
path := credentialsConfigFile(configDir)
|
||||
|
||||
file, err := os.Create(path)
|
||||
func UpdateCurrentAccount(account string) error {
|
||||
authConfig, err := NewAuthenticationConfigFromFile()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to open %s; %w", path, err)
|
||||
return fmt.Errorf("unable to retrieve the existing authentication configuration; %w", err)
|
||||
}
|
||||
|
||||
if _, ok := authConfig.Credentials[account]; !ok {
|
||||
return fmt.Errorf("account %s is not found", account)
|
||||
}
|
||||
|
||||
authConfig.CurrentAccount = account
|
||||
|
||||
if err := saveCredentialsConfigFile(authConfig); err != nil {
|
||||
return fmt.Errorf("unable to save the authentication configuration to file; %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func credentialsConfigFile() string {
|
||||
return filepath.Join(configDir(), credentialsFileName)
|
||||
}
|
||||
|
||||
func configDir() string {
|
||||
rootDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
rootDir = "."
|
||||
}
|
||||
|
||||
return filepath.Join(rootDir, internal.ApplicationName)
|
||||
}
|
||||
|
||||
func ensureConfigDir() error {
|
||||
dir := configDir()
|
||||
|
||||
if _, err := os.Stat(dir); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
||||
return fmt.Errorf("unable to create %s; %w", dir, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("unknown error received when running stat on %s; %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveCredentialsConfigFile(authConfig CredentialsConfig) error {
|
||||
file, err := os.Create(credentialsConfigFile())
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to open the config file; %w", err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
@ -135,7 +153,3 @@ func saveCredentialsConfigFile(authConfig CredentialsConfig, configDir string) e
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func credentialsConfigFile(configDir string) string {
|
||||
return filepath.Join(calculateConfigDir(configDir), credentialsFileName)
|
||||
}
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
||||
)
|
||||
|
||||
func calculateConfigDir(configDir string) string {
|
||||
if configDir != "" {
|
||||
return configDir
|
||||
}
|
||||
|
||||
rootDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
rootDir = "."
|
||||
}
|
||||
|
||||
return filepath.Join(rootDir, internal.ApplicationName)
|
||||
}
|
||||
|
||||
func ensureConfigDir(configDir string) error {
|
||||
if _, err := os.Stat(configDir); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
if err := os.MkdirAll(configDir, 0o750); err != nil {
|
||||
return fmt.Errorf("unable to create %s; %w", configDir, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("unknown error received when running stat on %s; %w", configDir, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in a new issue