-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for autodiscovery of repos from a list of organizations and users. Changed configuration from env variables to a flat yaml file. Updated readme to support new changes. Changed how the output looks, added emojis.
- Loading branch information
Showing
13 changed files
with
429 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Config : configuration for what orgs / users / repos and token to use | ||
type Config struct { | ||
AutoDiscover struct { | ||
Organizations []struct { | ||
Name string `yaml:"name"` | ||
Topic string `yaml:"topic"` | ||
} `yaml:"organizations"` | ||
Users []struct { | ||
Name string `yaml:"name"` | ||
Topic string `yaml:"topic"` | ||
} `yaml:"users"` | ||
} `yaml:"autoDiscover"` | ||
SubscribedRepos []string `yaml:"subscribedRepos"` | ||
Token string `yaml:"token"` | ||
} | ||
|
||
func ensureConfigDirExists(configDir string) error { | ||
// Check if config directory exists | ||
_, err := os.Stat(configDir) | ||
if os.IsNotExist(err) { | ||
// Config directory doesn't exist, create it | ||
err := os.MkdirAll(configDir, 0700) // 0700 means only the owner can read, write, and execute | ||
if err != nil { | ||
return fmt.Errorf("failed to create config directory: %v", err) | ||
} | ||
} else if err != nil { | ||
// Some error occurred while checking the existence of the directory | ||
return fmt.Errorf("failed to check config directory: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ensureConfigFileExists(configFilePath string) error { | ||
_, err := os.Stat(configFilePath) | ||
if os.IsNotExist(err) { | ||
// Config file doesn't exist, create it with default values | ||
defaultConfig := Config{ | ||
SubscribedRepos: []string{}, | ||
Token: "", | ||
} | ||
configBytes, err := yaml.Marshal(defaultConfig) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal default config: %v", err) | ||
} | ||
err = os.WriteFile(configFilePath, configBytes, 0600) // 0600 means only the owner can read and write | ||
if err != nil { | ||
return fmt.Errorf("failed to create config file: %v", err) | ||
} | ||
} else if err != nil { | ||
// Some error occurred while checking the existence of the file | ||
return fmt.Errorf("failed to check config file: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getConfigFilePath() (string, error) { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
configDir := filepath.Join(homeDir, ".config/ghreport") | ||
if err := ensureConfigDirExists(configDir); err != nil { | ||
return "", err | ||
} | ||
configFilePath := filepath.Join(configDir, "config.yaml") | ||
if err := ensureConfigFileExists(configFilePath); err != nil { | ||
return "", err | ||
} | ||
return configFilePath, nil | ||
} | ||
|
||
func readConfigFile(configFilePath string) (*Config, error) { | ||
file, err := os.Open(configFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
var config Config | ||
decoder := yaml.NewDecoder(file) | ||
if err := decoder.Decode(&config); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &config, nil | ||
} | ||
|
||
func getConfig() (*Config, error) { | ||
// Check if config file exists | ||
configFilePath, err := getConfigFilePath() | ||
if err == nil { | ||
if _, err := os.Stat(configFilePath); err == nil { | ||
// Config file exists, read values from there | ||
config, err := readConfigFile(configFilePath) | ||
if err == nil { | ||
return config, nil | ||
} | ||
} | ||
} | ||
|
||
// If config file doesn't exist or there was an error reading it, fallback to environment variables | ||
var config Config | ||
envSubscribedRepos := os.Getenv("subscribedRepos") | ||
if envSubscribedRepos == "" { | ||
return &config, fmt.Errorf("env variable subscribedRepos is not defined") | ||
} | ||
config.SubscribedRepos = strings.Split(envSubscribedRepos, " ") | ||
envToken := os.Getenv("ghreportToken") | ||
if envToken == "" { | ||
return &config, fmt.Errorf("env variable ghreportToken is not defined") | ||
} | ||
config.Token = envToken | ||
|
||
return &config, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
module github.com/jmainguy/ghreport | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/shurcooL/githubv4 v0.0.0-20240120211514-18a1ae0e79dc | ||
github.com/stretchr/testify v1.8.4 | ||
golang.org/x/oauth2 v0.17.0 | ||
golang.org/x/oauth2 v0.18.0 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/golang/protobuf v1.5.4 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect | ||
github.com/stretchr/objx v0.5.0 // indirect | ||
google.golang.org/appengine v1.6.8 // indirect | ||
google.golang.org/protobuf v1.32.0 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.