Skip to content

Commit fbd4b3e

Browse files
committed
Reading users and generating list
1 parent 90a4169 commit fbd4b3e

File tree

5 files changed

+237
-136
lines changed

5 files changed

+237
-136
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/
22
.env
3+
USERS.md

config/config.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package config
2+
3+
import (
4+
"flag"
5+
"log"
6+
"log/slog"
7+
"os"
8+
"strconv"
9+
)
10+
11+
const (
12+
keyAction = "ACTION"
13+
keyEnterprise = "ENTERPRISE"
14+
keyGithubToken = "GITHUB_TOKEN"
15+
keyTemplateFile = "TEMPLATE_FILE"
16+
keyVerbose = "VERBOSE"
17+
keyMarkdownFile = "MARKDOWN_FILE"
18+
)
19+
20+
type Config struct {
21+
Action string
22+
Enterprise string
23+
GithubToken string
24+
TemplateFile string
25+
MarkdownFile string
26+
}
27+
28+
func New() (*Config, error) {
29+
c := Config{}
30+
flag.StringVar(&c.Action, keyAction, lookupEnvOrString("ACTION", ""), "The action to perform.")
31+
flag.StringVar(&c.Enterprise, keyEnterprise, lookupEnvOrString("ENTERPRISE", ""), "The GitHub Enterprise to query for repositories.")
32+
flag.StringVar(&c.GithubToken, keyGithubToken, lookupEnvOrString("GITHUB_TOKEN", ""), "The GitHub Token to use for authentication.")
33+
flag.StringVar(&c.TemplateFile, keyTemplateFile, lookupEnvOrString("TEMPLATE_FILE", "template/userlist.tpl"), "The template file to use for rendering the result.")
34+
flag.StringVar(&c.MarkdownFile, keyMarkdownFile, lookupEnvOrString("MARKDOWN_FILE", "USERS.md"), "The markdown file to write the result to.")
35+
verbose := flag.Int("verbose", lookupEnvOrInt(keyVerbose, 0), "Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE.")
36+
37+
logLevel := &slog.LevelVar{}
38+
if verbose != nil {
39+
if *verbose == 0 {
40+
logLevel.Set(slog.LevelInfo)
41+
} else {
42+
logLevel.Set(slog.LevelDebug)
43+
}
44+
}
45+
flag.Parse()
46+
return &c, nil
47+
}
48+
49+
func lookupEnvOrString(key string, defaultVal string) string {
50+
if val, ok := os.LookupEnv(key); ok {
51+
return val
52+
}
53+
return defaultVal
54+
}
55+
56+
func lookupEnvOrInt(key string, defaultVal int) int {
57+
if val, ok := os.LookupEnv(key); ok {
58+
v, err := strconv.Atoi(val)
59+
if err != nil {
60+
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
61+
}
62+
return v
63+
}
64+
return defaultVal
65+
}

main.go

+28-126
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,63 @@
11
package main
22

33
import (
4-
"flag"
4+
config2 "github.com/prodyna/github-users/config"
55
"github.com/prodyna/github-users/userlist"
66
"log/slog"
77
"os"
88
)
99

1010
const (
11+
keyAction = "ACTION"
1112
keyOrganization = "ORGANIZATION"
1213
keyGithubToken = "GITHUB_TOKEN"
1314
keyTemplateFile = "TEMPLATE_FILE"
1415
)
1516

16-
func main() {
17-
userlistCmd := flag.NewFlagSet("userlist", flag.ExitOnError)
18-
userlistEnterprise := userlistCmd.String("enterprise", "", "The GitHub Enterprise to query for repositories.")
19-
ueerlistGithubToken := userlistCmd.String("github-token", "", "The GitHub Token to use for authentication.")
20-
ueerlistTemplateFile := userlistCmd.String("template-file", "template/userlist.tpl", "The template file to use for rendering the result.")
17+
type Config struct {
18+
Action string
19+
Enterprise string
20+
GithubToken string
21+
TemplateFile string
22+
}
2123

22-
if len(os.Args) < 2 {
23-
slog.Error("expected command 'userlist'")
24+
func main() {
25+
config, err := config2.New()
26+
if err != nil {
27+
slog.Error("Unable to create config", "error", err)
2428
os.Exit(1)
2529
}
2630

27-
switch os.Args[1] {
31+
switch config.Action {
2832
case "userlist":
29-
userlistCmd.Parse(os.Args[2:])
30-
ul := userlist.New(
31-
userlist.WithEnterprise(*userlistEnterprise),
32-
userlist.WithGithubToken(*ueerlistGithubToken),
33-
userlist.WithTemplate(*ueerlistTemplateFile),
33+
ulc := userlist.New(
34+
userlist.WithEnterprise(config.Enterprise),
35+
userlist.WithGithubToken(config.GithubToken),
36+
userlist.WithTemplateFile(config.TemplateFile),
37+
userlist.WithMarkdownFile(config.MarkdownFile),
3438
)
35-
err := ul.Validate()
39+
err := ulc.Validate()
3640
if err != nil {
37-
slog.Error("Invalid configuration", "error", err)
38-
userlistCmd.PrintDefaults()
41+
slog.Error("Invalid config", "error", err)
3942
os.Exit(1)
4043
}
41-
err = ul.Load()
44+
err = ulc.Load()
4245
if err != nil {
4346
slog.Error("Unable to load userlist", "error", err)
4447
os.Exit(1)
4548
}
46-
err = ul.Render()
49+
err = ulc.Print()
50+
if err != nil {
51+
slog.Error("Unable to print userlist", "error", err)
52+
os.Exit(1)
53+
}
54+
err = ulc.Render()
4755
if err != nil {
4856
slog.Error("Unable to render userlist", "error", err)
4957
os.Exit(1)
5058
}
5159
default:
52-
slog.Error("expected command")
60+
slog.Error("Unknown action", "action", config.Action)
5361
os.Exit(1)
5462
}
55-
56-
//ctx := context.Background()
57-
//src := oauth2.StaticTokenSource(
58-
// &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
59-
//)
60-
//httpClient := oauth2.NewClient(ctx, src)
61-
//
62-
//client := githubv4.NewClient(httpClient)
63-
//
64-
///*
65-
// {
66-
// enterprise(slug: "prodyna") {
67-
// ownerInfo {
68-
// samlIdentityProvider {
69-
// externalIdentities(after: null, first: 100) {
70-
// pageInfo {
71-
// hasNextPage
72-
// endCursor
73-
// }
74-
// edges {
75-
// node {
76-
// user {
77-
// login
78-
// }
79-
// samlIdentity {
80-
// nameId
81-
// }
82-
// }
83-
// }
84-
// }
85-
// }
86-
// }
87-
// }
88-
// }
89-
//*/
90-
//var query struct {
91-
// Enterprise struct {
92-
// Slug string
93-
// Name string
94-
// OwnerInfo struct {
95-
// SamlIdentityProvider struct {
96-
// ExternalIdentities struct {
97-
// PageInfo struct {
98-
// HasNextPage bool
99-
// EndCursor githubv4.String
100-
// }
101-
// Edges []struct {
102-
// Node struct {
103-
// User struct {
104-
// Login string
105-
// Organizations struct {
106-
// Nodes []struct {
107-
// Name string
108-
// }
109-
// }
110-
// }
111-
// SamlIdentity struct {
112-
// NameId string
113-
// }
114-
// }
115-
// }
116-
// } `graphql:"externalIdentities(after: $after, first: $first)"`
117-
// }
118-
// }
119-
// /*
120-
// Members struct {
121-
// TotalCount int
122-
// Nodes []struct {
123-
// EnterpriseUserAccount struct {
124-
// Login string
125-
// Name string
126-
// User struct {
127-
// Login string
128-
// Name string
129-
// }
130-
// } `graphql:"... on EnterpriseUserAccount"`
131-
// User struct {
132-
// Login string
133-
// } `graphql:"... on User"`
134-
// }
135-
// PageInfo struct {
136-
// EndCursor githubv4.String
137-
// HasNextPage bool
138-
// }
139-
// } `graphql:"members(first: $first)"`
140-
// */
141-
// } `graphql:"enterprise(slug: $slug)"`
142-
//}
143-
//
144-
//variables := map[string]interface{}{
145-
// "slug": githubv4.String("prodyna"),
146-
// "first": githubv4.Int(3),
147-
// "after": (*githubv4.String)(nil),
148-
//}
149-
//
150-
//err := client.Query(ctx, &query, variables)
151-
//if err != nil {
152-
// slog.ErrorContext(ctx, "Unable to query", "error", err)
153-
//}
154-
//
155-
//// print json string of the query result
156-
//output, err := json.MarshalIndent(query, "", " ")
157-
//if err != nil {
158-
// slog.ErrorContext(ctx, "Unable to marshal query result", "error", err)
159-
//}
160-
//fmt.Print(string(output))
16163
}

template/userlist.tpl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GitHub Enterprise Users
2+
3+
| # | GitHub Login | E-Mail |
4+
| --- | --- | --- |
5+
{{ range .Users }} | {{ .Number }} | [{{ .Login }}](https://github.com/enterprises/prodyna/people/{{ .Login }}/sso) | {{ .Email }} |
6+
{{end}}

0 commit comments

Comments
 (0)