Skip to content

store config in $XDG_CONFIG_DIR $HOME/.config/trakt-cli/config.yaml #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/sirupsen/logrus"
"github.com/adrg/xdg"
"gopkg.in/yaml.v3"
)

Expand All @@ -33,20 +32,18 @@ type Credentials struct {
// Create a new API client for the given API version.
func NewAPIClient() APIClient {

// read ~/.trakt.yaml file
homeDir, err := os.UserHomeDir()
configFile, err := xdg.SearchConfigFile("trakt-cli/config.yaml")
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to read %q file, please run `trakt auth`", configFile)
}
out, err := ioutil.ReadFile(homeDir + "/.trakt.yaml")
config, err := os.ReadFile(configFile)
if err != nil {
logrus.WithError(err).Error("Failed to read ~/.trakt.yaml file, please run `trakt auth`")
log.Fatal(err)
}

var creds Credentials
err = yaml.Unmarshal(out, &creds)
err = yaml.Unmarshal(config, &creds)
if err != nil {
logrus.WithError(err).Error("Failed to read ~/.trakt.yaml file, please run `trakt auth`")
log.Fatalf("Failed to read %q file, please run `trakt auth`", configFile)
}

return APIClient{
Expand Down
18 changes: 8 additions & 10 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"
"time"

"github.com/angristan/trakt-cli/api"
"github.com/briandowns/spinner"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

"github.com/adrg/xdg"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +32,7 @@ var authCmd = &cobra.Command{
ClientID: cmd.Flag("client-id").Value.String(),
})
if err != nil {
logrus.WithError(err).Fatal("Failed to get device code1")
log.Fatalf("Failed to get device code: %v\n", err)
return
}

Expand All @@ -50,7 +49,7 @@ var authCmd = &cobra.Command{
ClientSecret: cmd.Flag("client-secret").Value.String(),
})
if err != nil {
logrus.WithError(err).Fatal("Failed to get device code")
log.Fatalf("Failed to get device code: %s\n", err)
return
}
if len(tokenResp.AccessToken) == 0 {
Expand All @@ -67,18 +66,17 @@ var authCmd = &cobra.Command{
fmt.Printf("Error while Marshaling. %v", err)
}

// write to ~/.trakt.yaml
homeDir, err := os.UserHomeDir()
configFile, err := xdg.ConfigFile("trakt-cli/config.yaml")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(homeDir+"/.trakt.yaml", yamlData, 0644)
err = os.WriteFile(configFile, yamlData, 0644)
if err != nil {
fmt.Printf("Error while writing to file. %v", err)
}

s.Stop()
fmt.Printf("Successfully authenticated, creds written to ~/.trakt.yaml\n")
log.Printf("Successfully authenticated, creds written to %q\n", configFile)

break
}
Expand All @@ -94,10 +92,10 @@ func init() {

err := authCmd.MarkPersistentFlagRequired("client-id")
if err != nil {
logrus.WithError(err).Fatal("Failed to mark client-id flag required")
log.Fatalf("Failed to mark client-id flag required: %v\n", err)
}
err = authCmd.MarkPersistentFlagRequired("client-secret")
if err != nil {
logrus.WithError(err).Fatal("Failed to mark client-secret flag required")
log.Fatalf("Failed to mark client-secret flag required: %v\n", err)
}
}
8 changes: 4 additions & 4 deletions cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"
"os"
"time"

Expand All @@ -10,7 +11,6 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/mergestat/timediff"
"github.com/muesli/termenv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -28,16 +28,16 @@ var historyCmd = &cobra.Command{

settings, err := client.GetUserSettings()
if err != nil {
logrus.WithError(err).Fatal("Failed to get user settings")
log.Fatalf("Failed to get user settings: %v\n", err)
}

page, err := cmd.Flags().GetInt("page")
if err != nil {
logrus.WithError(err).Fatal("Failed to get page")
log.Fatalf("Failed to get page: %v\n", err)
}
limit, err := cmd.Flags().GetInt("limit")
if err != nil {
logrus.WithError(err).Fatal("Failed to get limit")
log.Fatalf("Failed to get limit: %v\n", err)
}

resp, pagination, err := client.GetUserHistory(settings.User.Ids.Slug, api.PaginationsParams{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/angristan/trakt-cli
go 1.17

require (
github.com/adrg/xdg v0.4.0
github.com/briandowns/spinner v1.18.1
github.com/jedib0t/go-pretty/v6 v6.2.7
github.com/mergestat/timediff v0.0.3
github.com/muesli/termenv v0.11.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.3.0
)

Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -321,8 +323,6 @@ github.com/sagikazarmark/crypt v0.3.0/go.mod h1:uD/D+6UF4SrIR1uGEv7bBNkNqLGqUr43
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down Expand Up @@ -542,6 +542,7 @@ golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down