Skip to content

Commit 5426af0

Browse files
committed
[cli] Use settings to set the GitHub authentication
1 parent 3094f0c commit 5426af0

File tree

3 files changed

+66
-24
lines changed

3 files changed

+66
-24
lines changed

cli/src/Cli.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Core.Types.Tx (TxHash, WithTxHash (..))
1212
import Core.Types.Wallet (Wallet)
1313
import Data.Functor.Identity (Identity (..))
1414
import Facts (FactsSelection, factsCmd)
15-
import Lib.GitHub (getOAUth)
15+
import GitHub (Auth)
1616
import Lib.JSON.Canonical.Extra
1717
import MPFS.API
1818
( MPFS (..)
@@ -48,15 +48,18 @@ import Validation (mkValidation)
4848
import Wallet.Cli (WalletCommand, walletCmd)
4949

5050
data Command a where
51-
RequesterCommand :: MPFSClient -> RequesterCommand a -> Command a
52-
OracleCommand :: MPFSClient -> OracleCommand a -> Command a
53-
AgentCommand :: MPFSClient -> AgentCommand NotReady a -> Command a
51+
RequesterCommand
52+
:: Auth -> MPFSClient -> RequesterCommand a -> Command a
53+
OracleCommand :: Auth -> MPFSClient -> OracleCommand a -> Command a
54+
AgentCommand
55+
:: Auth -> MPFSClient -> AgentCommand NotReady a -> Command a
5456
RetractRequest
5557
:: MPFSClient -> Wallet -> RequestRefId -> Command TxHash
5658
GetFacts :: MPFSClient -> TokenId -> FactsSelection a -> Command a
5759
Wallet :: WalletCommand a -> Command a
5860
GetToken
59-
:: MPFSClient
61+
:: Auth
62+
-> MPFSClient
6063
-> TokenId
6164
-> Command
6265
(AValidationResult TokenInfoFailure (Token WithValidation))
@@ -67,29 +70,29 @@ data SetupError = TokenNotSpecified
6770
cmd :: Command a -> IO a
6871
cmd = \case
6972
RequesterCommand
73+
auth
7074
MPFSClient{runMPFS, submitTx}
7175
requesterCommand -> do
72-
auth <- getOAUth
7376
runMPFS
7477
$ withContext
7578
mpfsClient
7679
(mkValidation auth)
7780
submitTx
7881
$ requesterCmd requesterCommand
7982
OracleCommand
83+
auth
8084
MPFSClient{runMPFS, submitTx}
8185
oracleCommand -> do
82-
auth <- getOAUth
8386
runMPFS
8487
$ withContext
8588
mpfsClient
8689
(mkValidation auth)
8790
submitTx
8891
$ oracleCmd oracleCommand
8992
AgentCommand
93+
auth
9094
MPFSClient{runMPFS, submitTx}
9195
agentCommand -> do
92-
auth <- getOAUth
9396
runMPFS
9497
$ withContext
9598
mpfsClient
@@ -110,9 +113,9 @@ cmd = \case
110113
runMPFS $ factsCmd mpfsClient tokenId factsCommand
111114
Wallet walletCommand -> liftIO $ walletCmd walletCommand
112115
GetToken
116+
auth
113117
MPFSClient{runMPFS, submitTx}
114118
tk -> do
115-
auth <- getOAUth
116119
runMPFS
117120
$ withContext
118121
mpfsClient

cli/src/Lib/GitHub.hs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module Lib.GitHub
88
, githubGetFile
99
, githubGetCodeOwnersFile
1010
, githubRepositoryExists
11-
, getOAUth
1211
) where
1312

1413
import Control.Exception
@@ -22,7 +21,6 @@ import Core.Types.Basic
2221
, Username (..)
2322
)
2423
import Data.ByteString.Base64 qualified as B64
25-
import Data.ByteString.Char8 qualified as BC
2624
import Data.Foldable (Foldable (..))
2725
import Data.Text qualified as T
2826
import Data.Text.Encoding qualified as T
@@ -36,14 +34,8 @@ import Network.HTTP.Client
3634
, Response (..)
3735
)
3836
import Network.HTTP.Types (Status (..))
39-
import System.Environment (getEnv)
4037
import Text.JSON.Canonical
4138

42-
getOAUth :: IO Auth
43-
getOAUth = do
44-
tk <- BC.pack <$> getEnv "GITHUB_PERSONAL_ACCESS_TOKEN"
45-
return $ OAuth tk
46-
4739
data GithubResponseError
4840
= GithubResponseErrorRepositoryNotFound
4941
| GithubResponseErrorSSHPublicKeysCannotBeFetched String

cli/src/Options.hs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,32 @@ import Core.Options
2020
, walletOption
2121
)
2222
import Core.Types.MPFS (mpfsClientOption)
23+
import Core.Types.Mnemonics.Options (queryConsole)
24+
import Data.ByteString.Char8 qualified as B
25+
import Data.Functor (($>))
26+
import Data.Text (Text)
27+
import Data.Text qualified as T
28+
import Data.Text.Encoding qualified as T
2329
import Data.Version (Version)
2430
import Facts (FactsSelection (..), TestRunSelection (..))
31+
import GitHub (Auth (..))
2532
import Lib.Box (Box (..), fmapBox)
2633
import MPFS.API (mpfsClient)
2734
import OptEnvConf
2835
( Parser
36+
, auto
2937
, command
3038
, commands
39+
, env
40+
, help
41+
, long
42+
, mapIO
43+
, metavar
44+
, option
45+
, reader
3146
, runParser
47+
, setting
48+
, str
3249
, (<|>)
3350
)
3451
import Oracle.Options (oracleCommandParser)
@@ -40,20 +57,47 @@ newtype Options a = Options
4057
{ optionsCommand :: Command a
4158
}
4259

60+
githubAuthOption :: Parser Auth
61+
githubAuthOption =
62+
fmap OAuth
63+
$ mapIO id
64+
$ setting
65+
[ help "Prompt for the passphrase for the encrypted mnemonics"
66+
, env "ANTI_INTERACTIVE_PASSWORD"
67+
, metavar "NONE"
68+
, long "ask-passphrase"
69+
, option
70+
, reader
71+
$ str @String
72+
$> ( T.encodeUtf8
73+
<$> queryConsole "Enter your GitHub personal access token: "
74+
)
75+
]
76+
<|> setting
77+
[ env "GITHUB_PERSONAL_ACCESS_TOKEN"
78+
, metavar "PASSPHRASE"
79+
, help
80+
"A GitHub personal access token with access to public repositories"
81+
, reader $ fmap (pure . B.pack) str
82+
]
83+
4384
commandParser :: Parser (Box Command)
4485
commandParser =
4586
commands
4687
[ command "oracle" "Manage oracle operations"
47-
$ (\c -> fmapBox (OracleCommand c))
48-
<$> mpfsClientOption
88+
$ (\a c -> fmapBox (OracleCommand a c))
89+
<$> githubAuthOption
90+
<*> mpfsClientOption
4991
<*> oracleCommandParser
5092
, command "requester" "Manage requester operations"
51-
$ (\c -> fmapBox (RequesterCommand c))
52-
<$> mpfsClientOption
93+
$ (\a c -> fmapBox (RequesterCommand a c))
94+
<$> githubAuthOption
95+
<*> mpfsClientOption
5396
<*> requesterCommandParser
5497
, command "agent" "Manage agent operations"
55-
$ (\c -> fmapBox (AgentCommand c))
56-
<$> mpfsClientOption
98+
$ (\a c -> fmapBox (AgentCommand a c))
99+
<$> githubAuthOption
100+
<*> mpfsClientOption
57101
<*> agentCommandParser
58102
, command "wallet" "Manage wallet operations"
59103
$ fmapBox Wallet <$> walletCommandParser
@@ -67,7 +111,10 @@ commandParser =
67111
<*> tokenIdOption
68112
<*> factsSelectionParser
69113
, command "token" "Get the token content"
70-
$ fmap Box . GetToken <$> mpfsClientOption <*> tokenIdOption
114+
$ (\a -> fmap Box . GetToken a)
115+
<$> githubAuthOption
116+
<*> mpfsClientOption
117+
<*> tokenIdOption
71118
]
72119

73120
factsSelectionParser :: Parser (Box FactsSelection)

0 commit comments

Comments
 (0)