Skip to content

Commit

Permalink
feat: support sort-by and order flags (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecobask authored Nov 8, 2023
1 parent 6eb8592 commit 5fb68f2
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 113 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build:
go build -o build/instagram main.go

docs:
rm -rf docs/*
go run cmd/docs/main.go

fmt:
Expand Down
7 changes: 4 additions & 3 deletions cmd/followdata/followers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
const CommandNameFollowers = "followers"

func NewFollowersCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: CommandNameFollowers,
Short: "Retrieve a list of users who follow you",
RunE: func(cmd *cobra.Command, args []string) error {
output, err := cmd.Flags().GetString(instagram.FlagOutput)
opts, err := instagram.NewOptions(cmd.Flags())
if err != nil {
return err
}
opts := instagram.NewOptions(output)
if err = opts.Validate(); err != nil {
return err
}
Expand All @@ -30,4 +29,6 @@ func NewFollowersCommand() *cobra.Command {
},
DisableAutoGenTag: true,
}
addCommonFlags(cmd)
return cmd
}
7 changes: 4 additions & 3 deletions cmd/followdata/following.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
const CommandNameFollowing = "following"

func NewFollowingCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: CommandNameFollowing,
Short: "Retrieve a list of users who you follow",
RunE: func(cmd *cobra.Command, args []string) error {
output, err := cmd.Flags().GetString(instagram.FlagOutput)
opts, err := instagram.NewOptions(cmd.Flags())
if err != nil {
return err
}
opts := instagram.NewOptions(output)
if err = opts.Validate(); err != nil {
return err
}
Expand All @@ -30,4 +29,6 @@ func NewFollowingCommand() *cobra.Command {
},
DisableAutoGenTag: true,
}
addCommonFlags(cmd)
return cmd
}
17 changes: 11 additions & 6 deletions cmd/followdata/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ func NewRootCommand() *cobra.Command {
},
DisableAutoGenTag: true,
}
cmd.AddCommand(NewFollowersCommand())
cmd.AddCommand(NewFollowingCommand())
cmd.AddCommand(NewUnfollowersCommand())
for _, childCmd := range cmd.Commands() {
childCmd.Flags().StringP(instagram.FlagOutput, "o", instagram.OutputTable, `output format ("json", "table", "yaml")`)
}
cmd.AddCommand(
NewFollowersCommand(),
NewFollowingCommand(),
NewUnfollowersCommand(),
)
return cmd
}

func addCommonFlags(cmd *cobra.Command) {
cmd.Flags().String(instagram.FlagOrder, instagram.OrderDesc, `order direction ("asc", "desc")`)
cmd.Flags().String(instagram.FlagOutput, instagram.OutputTable, `output format ("json", "table", "yaml")`)
cmd.Flags().String(instagram.FlagSortBy, instagram.FieldTimestamp, `sort by field ("timestamp", "username")`)
}
7 changes: 4 additions & 3 deletions cmd/followdata/unfollowers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
const CommandNameUnfollowers = "unfollowers"

func NewUnfollowersCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: CommandNameUnfollowers,
Short: "Retrieve a list of users who are not following you back",
RunE: func(cmd *cobra.Command, args []string) error {
output, err := cmd.Flags().GetString(instagram.FlagOutput)
opts, err := instagram.NewOptions(cmd.Flags())
if err != nil {
return err
}
opts := instagram.NewOptions(output)
if err = opts.Validate(); err != nil {
return err
}
Expand All @@ -30,4 +29,6 @@ func NewUnfollowersCommand() *cobra.Command {
},
DisableAutoGenTag: true,
}
addCommonFlags(cmd)
return cmd
}
6 changes: 4 additions & 2 deletions cmd/information/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func NewRootCommand() *cobra.Command {
},
DisableAutoGenTag: true,
}
cmd.AddCommand(NewLoadCommand())
cmd.AddCommand(NewCleanupCommand())
cmd.AddCommand(
NewLoadCommand(),
NewCleanupCommand(),
)
return cmd
}
10 changes: 7 additions & 3 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ func NewRootCommand() *cobra.Command {
SilenceUsage: true,
DisableAutoGenTag: true,
}
cmd.SetHelpCommand(&cobra.Command{Hidden: true})
cmd.AddCommand(information.NewRootCommand())
cmd.AddCommand(followdata.NewRootCommand())
cmd.AddCommand(
information.NewRootCommand(),
followdata.NewRootCommand(),
)
cmd.SetHelpCommand(&cobra.Command{
Hidden: true,
})
return cmd
}
6 changes: 4 additions & 2 deletions docs/instagram_followdata_followers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ instagram followdata followers [flags]
### Options

```
-h, --help help for followers
-o, --output string output format ("json", "table", "yaml") (default "table")
-h, --help help for followers
--order string order direction ("asc", "desc") (default "desc")
--output string output format ("json", "table", "yaml") (default "table")
--sort-by string sort by field ("timestamp", "username") (default "timestamp")
```

### SEE ALSO
Expand Down
6 changes: 4 additions & 2 deletions docs/instagram_followdata_following.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ instagram followdata following [flags]
### Options

```
-h, --help help for following
-o, --output string output format ("json", "table", "yaml") (default "table")
-h, --help help for following
--order string order direction ("asc", "desc") (default "desc")
--output string output format ("json", "table", "yaml") (default "table")
--sort-by string sort by field ("timestamp", "username") (default "timestamp")
```

### SEE ALSO
Expand Down
6 changes: 4 additions & 2 deletions docs/instagram_followdata_unfollowers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ instagram followdata unfollowers [flags]
### Options

```
-h, --help help for unfollowers
-o, --output string output format ("json", "table", "yaml") (default "table")
-h, --help help for unfollowers
--order string order direction ("asc", "desc") (default "desc")
--output string output format ("json", "table", "yaml") (default "table")
--sort-by string sort by field ("timestamp", "username") (default "timestamp")
```

### SEE ALSO
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/cecobask/instagram-insights
go 1.21

require (
github.com/jedib0t/go-pretty/v6 v6.4.8
github.com/spf13/cobra v1.7.0
github.com/jedib0t/go-pretty/v6 v6.4.9
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.4.8 h1:HiNzyMSEpsBaduKhmK+CwcpulEeBrTmxutz4oX/oWkg=
github.com/jedib0t/go-pretty/v6 v6.4.8/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jedib0t/go-pretty/v6 v6.4.9 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWlB2Usc=
github.com/jedib0t/go-pretty/v6 v6.4.9/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
Expand All @@ -18,6 +22,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
14 changes: 10 additions & 4 deletions pkg/instagram/constants.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package instagram

const (
OutputJson = "json"
OutputNone = "none"
OutputTable = "table"
OutputYaml = "yaml"
FieldTimestamp = "timestamp"
FieldUsername = "username"
OrderAsc = "asc"
OrderDesc = "desc"
OutputJson = "json"
OutputNone = "none"
OutputTable = "table"
OutputYaml = "yaml"
)

const (
FlagOrder = "order"
FlagOutput = "output"
FlagSortBy = "sort-by"
GoogleDriveHost = "drive.google.com"
GoogleDriveParsedUrlFormat = "https://drive.google.com/u/0/uc?id=%s&export=download&confirm=t"
PathData = "instagram_data"
Expand Down
Loading

0 comments on commit 5fb68f2

Please sign in to comment.