Skip to content

Commit

Permalink
Implement CLI linking UI feedback (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
expede authored Mar 31, 2021
1 parent 0c406bf commit 37ddf27
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/artifacts-nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
name: 🗄️ Cache ~/.stack
with:
path: ~/.stack
key: ${{ matrix.os }}-stack-nix
key: ${{ matrix.os }}-stack-nix-2

- name: 🏗️ Build
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os:
- macos-10.15
# - macos-10.15
- ubuntu-18.04
- ubuntu-20.04

Expand Down
2 changes: 1 addition & 1 deletion fission-cli/library/Fission/CLI/Environment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ init ::
-> Maybe CID
-> m ()
init username fissionURL rootProof = do
logDebug @Text "🎛️ Initializing user config file"
logUser @Text "🎛️ Initializing user config file"

attempt Peers.getPeers >>= \case
Left err ->
Expand Down
6 changes: 3 additions & 3 deletions fission-cli/library/Fission/CLI/Environment/OS/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ data Supported

instance Display Supported where
display = \case
Linux -> "linux"
NixOS -> "nix"
MacOS -> "darwin"
Linux -> "Linux"
NixOS -> "Nix"
MacOS -> "macOS"
6 changes: 6 additions & 0 deletions fission-cli/library/Fission/CLI/Handler/App/Publish.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import Fission.Web.Client.Error

import qualified Fission.CLI.Display.Error as CLI.Error
import qualified Fission.CLI.Display.Success as CLI.Success

import qualified Fission.CLI.IPFS.Add as CLI.IPFS.Add
import Fission.CLI.IPFS.Daemon as IPFS.Daemon

import Fission.CLI.App.Environment as App
import Fission.CLI.Parser.Watch.Types
Expand All @@ -50,6 +52,7 @@ publish ::
, MonadCleanup m
, MonadLogger m
, MonadLocalIPFS m
, MonadIPFSDaemon m
, UCAN.MonadStore m
, MonadEnvironment m
, MonadWebClient m
Expand Down Expand Up @@ -81,6 +84,7 @@ publish watchFlag runner appURL appPath _updateDNS updateData = do -- TODO updat
Right App.Env {buildDir} -> do
absBuildPath <- liftIO $ makeAbsolute buildDir
logDebug $ "📱 Starting single IPFS add locally of " <> displayShow absBuildPath
logUser @Text "🛫 App publish local preflight"

CLI.IPFS.Add.dir absBuildPath >>= \case
Left err -> do
Expand All @@ -89,9 +93,11 @@ publish watchFlag runner appURL appPath _updateDNS updateData = do -- TODO updat

Right cid@(CID hash) -> do
logDebug $ "📱 Directory CID is " <> hash
_ <- IPFS.Daemon.runDaemon
proof <- getRootUserProof
req <- App.update appURL cid (Just updateData) <$> Client.attachAuth proof

logUser @Text "✈️ Pushing to remote"
retryOnStatus [status502] 100 req >>= \case
Left err -> do
CLI.Error.put err "Server unable to sync data"
Expand Down
7 changes: 4 additions & 3 deletions fission-cli/library/Fission/CLI/Handler/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,27 @@ setup ::
-> Maybe Email
-> m ()
setup maybeOS maybeUsername maybeEmail = do
logUser @Text "📥 Installing dependencies..."
logUser @Text "🌱 Setting up environment"
Executable.place maybeOS

attempt User.ensureNotLoggedIn >>= \case
Left _ ->
Display.putOk "Done! You're all ready to go 🚀"

Right () -> do
logUser @Text "🔑 Creating keys"
void . Key.Store.create $ Proxy @SigningKey
void . Key.Store.create $ Proxy @ExchangeKey

username <- do
Prompt.reaskYN "Do you have an existing account?" >>= \case
Prompt.reaskYN "🏠 Do you have an existing account?" >>= \case
False ->
User.register maybeUsername maybeEmail

True -> do
logUser @Text "🔗 Please open auth.fission.codes on a signed-in device"
signingSK <- Key.Store.fetch $ Proxy @SigningKey
rootURL <- getRemoteBaseUrl
Login.consume signingSK rootURL {baseUrlPath = "/user/link"} maybeUsername

logUser @Text "🏗️ Setting default config..."
Display.putOk $ "Done! Welcome to Fission, " <> textDisplay username <> ""
4 changes: 2 additions & 2 deletions fission-cli/library/Fission/CLI/Handler/User/Login.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ consume signingSK baseURL optUsername = do
username <- do
case optUsername of
Just uName -> return uName
Nothing -> ensure . mkUsername =<< reaskNotEmpty' "Please enter you username:"
Nothing -> ensure . mkUsername =<< reaskNotEmpty' "📛 Please enter your username:"

targetDID <- ensureM $ DID.getByUsername username
signingPK <- Key.Store.toPublic (Proxy @SigningKey) signingSK
Expand Down Expand Up @@ -201,7 +201,7 @@ consume signingSK baseURL optUsername = do
let
pinStep = PIN.Payload myDID pin

UTF8.putTextLn $ "Confirmation code: " <> textDisplay pin
logUser $ "🔢 Confirmation code: " <> textDisplay pin
secureBroadcastJSON aesConn pinStep

reattempt 100 do
Expand Down
2 changes: 1 addition & 1 deletion fission-cli/library/Fission/CLI/IPFS/Daemon.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ stop daemonProc = do
liftIO $ stopProcess daemonProc

forceStop :: MonadIO m => m ExitCode
forceStop = runProcess . fromString $ "killall fission-ipfs > /dev/null 2>&1"
forceStop = runProcess . fromString $ "2>/dev/null killall fission-ipfs > /dev/null"
4 changes: 3 additions & 1 deletion fission-cli/library/Fission/CLI/IPFS/Executable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ place' ::
=> OS.Supported
-> m ()
place' host = do
logDebug $ "Setting up IPFS binary for " <> textDisplay host
logUser $ "🪐 Downloading managed IPFS for " <> textDisplay host

IPFS.BinPath ipfsPath <- Path.globalIPFSBin
File.Serialized lazyFile <- ensureM . runBootstrapT . ipfsCat $ IPFS.binCidFor host
Expand All @@ -68,6 +68,8 @@ place' host = do

void . Turtle.chmod Turtle.executable $ Turtle.decodeString ipfsPath

logUser @Text "🎛️ Configuring managed IPFS"

IPFS.Config.init
void $ IPFS.Config.enableRelay

Expand Down
16 changes: 11 additions & 5 deletions fission-cli/library/Fission/CLI/Prompt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ module Fission.CLI.Prompt
, reaskWithError
) where

import qualified Data.List as List
import qualified RIO.List as List
import RIO.ByteString as BS hiding (map, pack)

import qualified Fission.Internal.UTF8 as UTF8
import Fission.Prelude

import qualified Fission.Internal.UTF8 as UTF8

reaskWithError ::
( MonadIO m
, MonadLogger m
Expand Down Expand Up @@ -52,10 +53,15 @@ reaskNotEmpty' prompt = do

-- | reask where valid responses are some form of yes/no
reaskYN :: (MonadIO m, MonadLogger m) => Text -> m Bool
reaskYN prompt = isYes <$> reask (prompt <> " [Y/n]") ynTest
reaskYN prompt = do
response <- reask (prompt <> " [Y/n]") validYn
return (isDefault response || isYes response)

validYn :: ByteString -> Bool
validYn resp = isYes resp || isNo resp || isDefault resp

ynTest :: ByteString -> Bool
ynTest resp = isYes resp || isNo resp
isDefault :: ByteString -> Bool
isDefault = (== "")

isYes :: ByteString -> Bool
isYes resp = List.elem resp (["y", "Y", "yes", "Yes", "YES"] :: [ByteString])
Expand Down
48 changes: 30 additions & 18 deletions fission-cli/library/Fission/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import qualified Crypto.PubKey.RSA as RSA
import qualified Crypto.PubKey.RSA.OAEP as RSA.OAEP
import Crypto.Random

import Data.ByteArray as ByteArray
import Data.ByteArray as ByteArray hiding (any)
import qualified Data.Yaml as YAML

import Control.Monad.Base
Expand Down Expand Up @@ -496,15 +496,16 @@ instance
runLocal opts' arg = do
logDebug @Text "🌌🎬 Running local IPFS"

when (requiresDaemon opts') do
void IPFS.Daemon.runDaemon

ipfsRepo <- globalIPFSRepo
IPFS.BinPath ipfs <- globalIPFSBin
IPFS.Timeout secs <- asks $ getField @"ipfsTimeout"

pwd <- getCurrentDirectory
ignorePath <- IPFS.Ignore.writeTmp . show . Crypto.hash @ByteString @SHA256 $ fromString pwd

void IPFS.Daemon.runDaemon

let
cidVersion = "--cid-version=1"
timeout = "--timeout=" <> show secs <> "s"
Expand All @@ -513,10 +514,10 @@ instance
arg' = Text.unpack . decodeUtf8Lenient $ Lazy.toStrict arg

(pipeArg, opts) =
if | cmd == Just "swarm" -> (Nothing, opts' <> [arg'])
| List.elem "--stdin-name" opts' -> (Just arg, opts' <> [timeout, cidVersion, ignore])
| cmd == Just "pin" || cmd == Just "add" -> (Nothing, opts' <> [arg', timeout, cidVersion, ignore])
| otherwise -> (Nothing, opts' <> [arg', timeout])
if | cmd == Just "swarm" -> (Nothing, opts' <> [arg'])
| List.elem "--stdin-name" opts' -> (Just arg, opts' <> [timeout, cidVersion, ignore])
| cmd == Just "pin" || cmd == Just "add" -> (Nothing, opts' <> [arg', timeout, cidVersion, ignore])
| otherwise -> (Nothing, opts' <> [arg', timeout])

processStr = intercalate " " ("IPFS_PATH=" <> ipfsRepo : ipfs : opts)
rawProcess = fromString processStr
Expand All @@ -540,6 +541,16 @@ instance
| otherwise ->
return . Left $ Process.UnknownErr stdErrs

requiresDaemon :: [String] -> Bool
requiresDaemon opts = or matchers
where
matchers :: [Bool]
matchers =
[ any ("swarm" `List.isPrefixOf`) opts
, any ("dht" `List.isPrefixOf`) opts
, any ("log" `List.isPrefixOf`) opts
]

instance
( HasLogFunc cfg
, HasField' "ipfsDaemonVar" cfg (MVar (Process () () ()))
Expand Down Expand Up @@ -582,11 +593,9 @@ instance

process <- startProcess . fromString $ intercalate " "
[ "IPFS_PATH=" <> ipfsRepo
, "2> /dev/null"
, ipfsPath
, "daemon"
, "--enable-pubsub-experiment"
, "--enable-namesys-pubsub"
, " > /dev/null 2>&1"
, "daemon > /dev/null"
]

logDebug @Text "😈🏁 IPFS daemon started"
Expand All @@ -601,9 +610,11 @@ instance
stopProcess process
void IPFS.Daemon.forceStop -- Clean up any existing, on the off chance

let lockPath = Turtle.decodeString $ ipfsRepo <> "/repo.lock"
void $ Turtle.touch lockPath
void $ Turtle.rm lockPath
let
lockPath = Turtle.decodeString $ ipfsRepo </> "repo.lock"

whenM (Turtle.testfile lockPath) do
void $ Turtle.rm lockPath

runDaemon

Expand All @@ -617,10 +628,10 @@ instance
command =
fromString $ intercalate " "
[ "IPFS_PATH=" <> ipfsRepo
, "2>/dev/null"
, ipfsPath
, "swarm"
, "addrs"
, "> /dev/null 2>&1"
, "addrs > /dev/null"
]

Turtle.export "IPFS_PATH" $ Text.pack ipfsRepo
Expand Down Expand Up @@ -746,7 +757,7 @@ instance forall errs cfg .
return . Left $ CannotDecrypt cryptoError

Right bs -> do
logDebug $ "Decrypted still-serialized brearer token: " <> bs
logDebug $ "Decrypted still-serialized bearer token: " <> bs
case eitherDecode $ encode ("Bearer " <> decodeUtf8Lenient bs) of
Left err -> return . Left $ UnableToDeserialize err
Right bearerToken -> return $ Right PubSub.Session {..}
Expand Down Expand Up @@ -787,7 +798,8 @@ instance
, MonadIPFSIgnore (FissionCLI errs cfg)
)
=> JWT.Resolver (FissionCLI errs cfg) where
resolve cid@(IPFS.CID hash') =
resolve cid@(IPFS.CID hash') = do
_ <- IPFS.Daemon.runDaemon
IPFS.runLocal ["cat"] (Lazy.fromStrict $ encodeUtf8 hash') <&> \case
Left errMsg ->
Left $ CannotResolve cid (ConnectionError $ toException errMsg)
Expand Down
2 changes: 1 addition & 1 deletion fission-cli/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fission-cli
version: '2.10.1.0'
version: '2.10.2.0'
category: CLI
author:
- Brooklyn Zelenka
Expand Down

0 comments on commit 37ddf27

Please sign in to comment.