@@ -12,7 +12,9 @@ use std::{env, error::Error, io};
1212
1313use structopt:: StructOpt ;
1414
15- use crate :: { aliaser:: add_self_alias, config:: ConfigFile , tool_cache:: ToolCache } ;
15+ use crate :: {
16+ aliaser:: add_self_alias, auth_store:: AuthStore , config:: ConfigFile , tool_cache:: ToolCache ,
17+ } ;
1618
1719#[ derive( Debug ) ]
1820struct ToolInvocation {
@@ -88,6 +90,21 @@ enum Subcommand {
8890
8991 /// List installed tools.
9092 List ,
93+
94+ /// Set the GitHub Personal Access Token that Foreman should use with the
95+ /// GitHub API.
96+ ///
97+ /// This token can also be configured by editing ~/.foreman/auth.toml.
98+ #[ structopt( name = "github-auth" ) ]
99+ GitHubAuth ( GitHubAuthCommand ) ,
100+ }
101+
102+ #[ derive( Debug , StructOpt ) ]
103+ struct GitHubAuthCommand {
104+ /// GitHub personal access token that Foreman should use.
105+ ///
106+ /// If not specified, Foreman will prompt for it.
107+ token : Option < String > ,
91108}
92109
93110fn actual_main ( ) -> io:: Result < ( ) > {
@@ -117,6 +134,30 @@ fn actual_main() -> io::Result<()> {
117134 }
118135 }
119136 }
137+ Subcommand :: GitHubAuth ( subcommand) => {
138+ let token = match subcommand. token {
139+ Some ( token) => token,
140+ None => {
141+ println ! ( "Foreman authenticates to GitHub using Personal Access Tokens." ) ;
142+ println ! ( "https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line" ) ;
143+ println ! ( ) ;
144+
145+ loop {
146+ let token = rpassword:: read_password_from_tty ( Some ( "GitHub Token: " ) ) ?;
147+
148+ if token. is_empty ( ) {
149+ println ! ( "Token must be non-empty." ) ;
150+ } else {
151+ break token;
152+ }
153+ }
154+ }
155+ } ;
156+
157+ AuthStore :: set_github_token ( & token) ?;
158+
159+ println ! ( "GitHub auth saved successfully." ) ;
160+ }
120161 }
121162
122163 Ok ( ( ) )
0 commit comments