|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "flag" |
| 4 | + config2 "github.com/prodyna/github-users/config" |
5 | 5 | "github.com/prodyna/github-users/userlist"
|
6 | 6 | "log/slog"
|
7 | 7 | "os"
|
8 | 8 | )
|
9 | 9 |
|
10 | 10 | const (
|
| 11 | + keyAction = "ACTION" |
11 | 12 | keyOrganization = "ORGANIZATION"
|
12 | 13 | keyGithubToken = "GITHUB_TOKEN"
|
13 | 14 | keyTemplateFile = "TEMPLATE_FILE"
|
14 | 15 | )
|
15 | 16 |
|
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 | +} |
21 | 23 |
|
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) |
24 | 28 | os.Exit(1)
|
25 | 29 | }
|
26 | 30 |
|
27 |
| - switch os.Args[1] { |
| 31 | + switch config.Action { |
28 | 32 | 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), |
34 | 38 | )
|
35 |
| - err := ul.Validate() |
| 39 | + err := ulc.Validate() |
36 | 40 | if err != nil {
|
37 |
| - slog.Error("Invalid configuration", "error", err) |
38 |
| - userlistCmd.PrintDefaults() |
| 41 | + slog.Error("Invalid config", "error", err) |
39 | 42 | os.Exit(1)
|
40 | 43 | }
|
41 |
| - err = ul.Load() |
| 44 | + err = ulc.Load() |
42 | 45 | if err != nil {
|
43 | 46 | slog.Error("Unable to load userlist", "error", err)
|
44 | 47 | os.Exit(1)
|
45 | 48 | }
|
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() |
47 | 55 | if err != nil {
|
48 | 56 | slog.Error("Unable to render userlist", "error", err)
|
49 | 57 | os.Exit(1)
|
50 | 58 | }
|
51 | 59 | default:
|
52 |
| - slog.Error("expected command") |
| 60 | + slog.Error("Unknown action", "action", config.Action) |
53 | 61 | os.Exit(1)
|
54 | 62 | }
|
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)) |
161 | 63 | }
|
0 commit comments