Skip to content

Commit

Permalink
feat: Implement account:company:list
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jan 9, 2022
1 parent 5a46114 commit 6f8c00d
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 105 deletions.
20 changes: 19 additions & 1 deletion account-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
)

type Client struct {
token token
token token
activeMembership *membership
memberships *[]membership
}

func (c Client) NewAuthenticatedRequest(method, path string, body io.Reader) (*http.Request, error) {
Expand All @@ -23,3 +25,19 @@ func (c Client) NewAuthenticatedRequest(method, path string, body io.Reader) (*h
return r, nil

}

func (c Client) GetActiveCompanyId() int {
return c.token.UserID
}

func (c Client) GetUserId() int {
return c.token.UserAccountID
}

func (c Client) GetActiveMembership() *membership {
return c.activeMembership
}

func (c Client) GetMemberships() *[]membership {
return c.memberships
}
90 changes: 0 additions & 90 deletions account-api/companies.go

This file was deleted.

93 changes: 92 additions & 1 deletion account-api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,62 @@ func NewApi(request LoginRequest) (*Client, error) {
return nil, fmt.Errorf("login: %v", err)
}

memberships, err := fetchMemberships(token)

if err != nil {
return nil, err
}

var activeMemberShip membership

for _, membership := range *memberships {
if membership.Company.Id == token.UserID {
activeMemberShip = membership
}
}

client := Client{
token: token,
token: token,
memberships: memberships,
activeMembership: &activeMemberShip,
}

return &client, nil
}

func fetchMemberships(token token) (*[]membership, error) {
r, err := http.NewRequest("GET", fmt.Sprintf("%s/account/%d/memberships", ApiUrl, token.UserAccountID), nil)
r.Header.Set("x-shopware-token", token.Token)

if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(r)

if err != nil {
return nil, err
}

defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("fetchMemberships: %v", err)
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf(string(data))
}

var companies []membership
if err := json.Unmarshal(data, &companies); err != nil {
return nil, fmt.Errorf("fetchMemberships: %v", err)
}

return &companies, nil
}

type token struct {
Token string `json:"token"`
Expire tokenExpire `json:"expire"`
Expand All @@ -62,3 +111,45 @@ type LoginRequest struct {
Email string `json:"shopwareId"`
Password string `json:"password"`
}

type membership struct {
Id int `json:"id"`
CreationDate string `json:"creationDate"`
Active bool `json:"active"`
Member struct {
Id int `json:"id"`
Email string `json:"email"`
AvatarUrl interface{} `json:"avatarUrl"`
PersonalData struct {
Id int `json:"id"`
Salutation struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
} `json:"salutation"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Locale struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
} `json:"locale"`
} `json:"personalData"`
} `json:"member"`
Company struct {
Id int `json:"id"`
Name string `json:"name"`
CustomerNumber int `json:"customerNumber"`
} `json:"company"`
Roles []struct {
Id int `json:"id"`
Name string `json:"name"`
CreationDate string `json:"creationDate"`
Company interface{} `json:"company"`
Permissions []struct {
Id int `json:"id"`
Context string `json:"context"`
Name string `json:"name"`
} `json:"permissions"`
} `json:"roles"`
}
30 changes: 30 additions & 0 deletions cmd/account_company_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
"strconv"
)

var accountCompanyListCmd = &cobra.Command{
Use: "account:company:list",
Short: "Lists all available company for your Account",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
client := getAccountApiByConfig()

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Customer ID"})

for _, membership := range *client.GetMemberships() {
table.Append([]string{strconv.FormatInt(int64(membership.Company.Id), 10), membership.Company.Name, strconv.FormatInt(int64(membership.Company.CustomerNumber), 10)})
}

table.Render()
},
}

func init() {
rootCmd.AddCommand(accountCompanyListCmd)
}
20 changes: 9 additions & 11 deletions cmd/account_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/viper"
"log"
"os"
shopware_account_api "shopware-cli/account-api"
accountApi "shopware-cli/account-api"
)

var loginCmd = &cobra.Command{
Expand All @@ -31,15 +31,15 @@ var loginCmd = &cobra.Command{
termColor.Blue("Using existing credentials. Use account:logout to logout")
}

client, err := shopware_account_api.NewApi(shopware_account_api.LoginRequest{Email: email, Password: password})
client, err := accountApi.NewApi(accountApi.LoginRequest{Email: email, Password: password})

if err != nil {
termColor.Red("Login failed with error: %s", err.Error())
os.Exit(1)
}

if newCredentials {

viper.Set("account_membership", client.GetActiveCompanyId())
err := viper.WriteConfig()

if err != nil {
Expand All @@ -53,14 +53,12 @@ var loginCmd = &cobra.Command{
log.Fatalln(err)
}

companies, err := client.Companies().MyCompanies()
if err != nil {
log.Fatalln(err)
}

fmt.Println(companies)

termColor.Green("Hey %s %s. You are now authenticated and can use all account commands", profile.PersonalData.FirstName, profile.PersonalData.LastName)
termColor.Green(
"Hey %s %s. You are now authenticated on company %s and can use all account commands",
profile.PersonalData.FirstName,
profile.PersonalData.LastName,
client.GetActiveMembership().Company.Name,
)
},
}

Expand Down
1 change: 0 additions & 1 deletion cmd/account_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
)

// logoutCmd represents the logout command
var logoutCmd = &cobra.Command{
Use: "account:logout",
Short: "Logout from Shopware Account",
Expand Down
17 changes: 16 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package cmd

import (
termColor "github.com/fatih/color"
"os"
accountApi "shopware-cli/account-api"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "shopware-cli",
Short: "A cli for common Shopware tasks",
Expand Down Expand Up @@ -50,3 +51,17 @@ func initConfig() {
if err := viper.ReadInConfig(); err == nil {
}
}

func getAccountApiByConfig() *accountApi.Client {
email := viper.GetString("account_email")
password := viper.GetString("account_password")

client, err := accountApi.NewApi(accountApi.LoginRequest{Email: email, Password: password})

if err != nil {
termColor.Red("Login failed with error: %s", err.Error())
os.Exit(1)
}

return client
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/fatih/color v1.13.0
github.com/manifoldco/promptui v0.9.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
)
Expand All @@ -17,6 +18,7 @@ require (
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/spf13/afero v1.6.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
Expand All @@ -285,6 +287,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
Expand Down

0 comments on commit 6f8c00d

Please sign in to comment.