diff --git a/README.md b/README.md index 1ddff24..0e0730d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,14 @@ I saw a GraphQL tutorial while trying to understand some code in the Teams codeb ## Usage -1. Add Github PAT with _user_ permissions to environment variable `GITHUB_PAT` or start the application with a token argument. +Option A: + +1. Add Github PAT with _user_ permissions to environment variable `GITHUB_PAT`. +2. Add Github username to the environment variable `GITHUB_USERNAME`. + +Option B: + +1. Pass the required Github PAT and username as the `t` and `u` arguments respectively. 2. Start the application. Arguments passed will override env variables. 3. (optional) If you're very weird you could run this as a system service I guess. Please let me know if you do, for science. diff --git a/main.go b/main.go index af2453d..580e7b4 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "context" _ "embed" "encoding/json" + "flag" "fmt" "log" "math" @@ -54,6 +55,7 @@ type authedTransport struct { var graphqlClient *graphql.Client var isWindows bool +var ghUserName string //go:embed appsStatus.json var jsonFile []byte @@ -169,7 +171,7 @@ func parseAppsFromFile() *AppList { func writeToGithubStatus(str string, emoji string, expiry time.Time) bool { fmt.Printf("Writing to Github status: %s\n", str) gqlReq := map[string]interface{}{ - "input": ChangeUserStatusInput{ClientMutationId: "darts/status", Message: str, Emoji: emoji, ExpiresAt: expiry}, + "input": ChangeUserStatusInput{ClientMutationId: fmt.Sprintf("%s/status", ghUserName), Message: str, Emoji: emoji, ExpiresAt: expiry}, } err := graphqlClient.Mutate(context.Background(), &statusMutation, gqlReq) if err != nil { @@ -228,21 +230,19 @@ func resetOnClose() { } func initClient() { - authTokenEnv := os.Getenv("GITHUB_PAT") + authToken := os.Getenv("GITHUB_PAT") + ghUserName := os.Getenv("GITHUB_USERNAME") - var authTokenCmd = "" - if len(os.Args) > 1 { - authTokenCmd = os.Args[1] - } + flag.StringVar(&authToken, "t", authToken, "your github auth token") + flag.StringVar(&ghUserName, "u", ghUserName, "your github username") + flag.Parse() - if len(authTokenEnv) < 1 && len(authTokenCmd) < 1 { - log.Fatal("GITHUB_PAT env variable not set & no token passed to app.\nUsage: status <token>") + if len(authToken) < 1 { + log.Fatal("GITHUB_PAT env variable not set & no token passed to app!") } - var authToken = authTokenEnv - - if len(authTokenCmd) > 0 { - authToken = authTokenCmd + if len(ghUserName) < 1 { + log.Fatal("GITHUB_USERNAME env variable not set & no username passed to app!") } httpClient := http.Client{