Skip to content
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

Add config option for default GitHub user/organization #7

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ var Search = &cobra.Command{
},
}

func AddCommands() {
func init() {
RootCmd.AddCommand(Search)
}
43 changes: 43 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
package cmd

import (
"log"
"strings"
"time"

"github.com/briandowns/spinner"
ui "github.com/gizak/termui/v3"
"github.com/spf13/cobra"

h "github.com/irevenko/octotui/helpers"
tui "github.com/irevenko/octotui/tui"
)

var RootCmd = &cobra.Command{
Use: "octotui",
Short: "GitHub stats in your terminal",
Long: `Complete documentation is available at https://github.com/irevenko/octotui`,
Run: func(cmd *cobra.Command, args []string) {
owner := h.LoadOwner()

if owner == "" {
log.Fatalf("Owner is empty. Either add data or use the search subcommand.")
}

nameAndType := strings.Split(owner, ":")

if len(nameAndType) != 2 {
log.Fatalf("Default owner must be in format \"name:type\" where type is either %q or %q", h.Org, h.User)
}

name := nameAndType[0]
ownerType := h.OwnerType(nameAndType[1])

s := spinner.New(spinner.CharSets[30], 100*time.Millisecond)
s.Prefix = "fetching github data "
s.FinalMSG = "done"
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
switch {
case ownerType.IsOrg():
s.Start()
tui.RenderOrganization(name, s)
case ownerType.IsUser():
s.Start()
tui.RenderUser(name, s)
default:
log.Fatalf("Expected either %q or %q, got %q in default_owner config file", h.Org, h.User, ownerType)
}
},
}
44 changes: 44 additions & 0 deletions helpers/default_owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package helpers

import (
"fmt"
"log"
)

const (
ownerFilename = "default_owner"
)

// OwnerType is the type of owner. Either "user" or "org".
type OwnerType string

const (
// Org signifies that the owner type is a GitHub organization.
Org OwnerType = "org"
// User signifies that the owner type is a GitHub user.
User OwnerType = "user"
)

// LoadOwner loads the config file for the default owner.
func LoadOwner() string {
owner, filepath, err := loadConfigFile(ownerFilename)

if err == errCreatedConfigFile {
fmt.Printf("Created owner file in: %v\n", filepath)
fmt.Println("Put your owner in this file in the format \"name:type\"")
fmt.Printf("Where type is either %q or %q\n", Org, User)
} else if err != nil {
log.Fatalf("Unable to load/create owner file: %v", err)
}
return owner
}

// IsOrg checks if owner is a GitHub organization.
func (owner OwnerType) IsOrg() bool {
return owner == Org
}

// IsUser checks if owner is a GitHub user.
func (owner OwnerType) IsUser() bool {
return owner == User
}
53 changes: 53 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package helpers

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)

var configPath = path.Join(".config", "octotui")

var errCreatedConfigFile error = errors.New("File created")

func loadConfigFile(filename string) (contents string, createdPath string, err error) {
home, err := os.UserHomeDir()

if err != nil {
err = fmt.Errorf("Unable to get home directory: %w", err)
return
}

fullConfigPath := path.Join(home, configPath)
fullFilename := path.Join(fullConfigPath, filename)

if _, innerErr := os.Stat(fullFilename); innerErr != nil {
if os.IsNotExist(innerErr) {
innerErr := os.Mkdir(fullConfigPath, 0755)
if innerErr != nil && !os.IsExist(innerErr) {
err = fmt.Errorf("Unable to create octotui folder in %v", fullConfigPath)
return
}

blackListFile, innerErr := os.OpenFile(fullFilename, os.O_RDONLY|os.O_CREATE, 0644)
if innerErr != nil {
err = fmt.Errorf("Unable to create file %v: %w", fullFilename, innerErr)
return
}
blackListFile.Close()

createdPath = fullFilename
err = errCreatedConfigFile
return
}
err = innerErr
return
}

fileContents, err := ioutil.ReadFile(fullFilename)
contents = strings.TrimSpace(string(fileContents))
return
}
39 changes: 8 additions & 31 deletions helpers/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,21 @@ package helpers

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)

const (
tokenPath = "/.config/octotui/token"
tokenFilename = "token"
)

func LoadToken() string {
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

if _, err := os.Stat(home + tokenPath); err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(home+"/.config/octotui", 0755)
if err != nil {
log.Fatal("Unable to create octotui folder in " + home + "/.config")
}

blackListFile, err := os.OpenFile(home+tokenPath, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
log.Fatal("Unable to create token file in " + home + tokenPath)
}
blackListFile.Close()
token, filepath, err := loadConfigFile(tokenFilename)

fmt.Println("Created token file in: " + home + tokenPath)
fmt.Println("Put your github token in this file")
}
if err == errCreatedConfigFile {
fmt.Printf("Created token file in: %v\n", filepath)
fmt.Println("Put your github token in this file")
} else if err != nil {
log.Fatalf("Unable to load/create token file: %v", err)
}

token, err := ioutil.ReadFile(home + tokenPath)
if err != nil {
log.Fatal("Can't read token file in: " + home + tokenPath)
}

return strings.TrimSpace(string(token))
return token
}
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

func main() {
cmd.AddCommands()

if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
8 changes: 4 additions & 4 deletions tui/render_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func RenderStats(username string, accType string, s *spinner.Spinner) {
defer ui.Close()

if accType == "(user)" {
renderUser(username, s)
RenderUser(username, s)
}

if accType == "(organization)" {
renderOrganization(username, s)
RenderOrganization(username, s)
}
}

func renderUser(username string, s *spinner.Spinner) {
func RenderUser(username string, s *spinner.Spinner) {
user, err := g.UserDetails(qlClient, username)
if err != nil {
log.Fatalf("Couldn't get user details for: %v: %v", username, err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func renderUser(username string, s *spinner.Spinner) {
}
}

func renderOrganization(username string, s *spinner.Spinner) {
func RenderOrganization(username string, s *spinner.Spinner) {
org, err := g.OrganizationDetails(qlClient, username)
if err != nil {
log.Fatalf("Couldn't get org details for: %v: %v", username, err)
Expand Down