Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Use built-in Console service (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 authored Sep 25, 2023
1 parent 2625bb1 commit 22dd35f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 124 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-hotels-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": minor
---

use builtin Console service
31 changes: 12 additions & 19 deletions examples/git.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Args from "@effect/cli/Args"
import * as CliApp from "@effect/cli/CliApp"
import * as Command from "@effect/cli/Command"
import * as Console from "@effect/cli/Console"
import * as HelpDoc from "@effect/cli/HelpDoc"
import * as Span from "@effect/cli/HelpDoc/Span"
import * as Options from "@effect/cli/Options"
Expand Down Expand Up @@ -88,25 +87,20 @@ const git: Command.Command<Git> = pipe(
Command.map(({ options: version, subcommand }) => Git({ version, subcommand }))
)

const handleRemoteSubcomand = (verbose: boolean) =>
(command: RemoteSubcommand): Effect.Effect<never, never, void> => {
switch (command._tag) {
case "AddRemote": {
const msg = (
`Executing 'git remote add' with '--name' set to '${command.name}', ` +
`'--url' set to '${command.url}', and '--verbose' set to ${verbose}`
)
return Effect.log(msg)
}
case "RemoveRemote": {
const msg = (
`Executing 'git remote remove' with '--name' set to '${command.name}', ` +
`and '--verbose' set to ${verbose}`
)
return Effect.log(msg)
}
const handleRemoteSubcomand = (verbose: boolean) => (command: RemoteSubcommand): Effect.Effect<never, never, void> => {
switch (command._tag) {
case "AddRemote": {
const msg = `Executing 'git remote add' with '--name' set to '${command.name}', ` +
`'--url' set to '${command.url}', and '--verbose' set to ${verbose}`
return Effect.log(msg)
}
case "RemoveRemote": {
const msg = `Executing 'git remote remove' with '--name' set to '${command.name}', ` +
`and '--verbose' set to ${verbose}`
return Effect.log(msg)
}
}
}

const handleGitSubcommand = (command: GitSubcommand): Effect.Effect<never, never, void> => {
switch (command._tag) {
Expand Down Expand Up @@ -143,6 +137,5 @@ pipe(
onSome: handleGitSubcommand
}))
),
Effect.provideLayer(Console.layer),
Effect.runFork
)
20 changes: 4 additions & 16 deletions src/CliApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @since 1.0.0
*/
import type { Command } from "@effect/cli/Command"
import type { Console } from "@effect/cli/Console"
import type { HelpDoc } from "@effect/cli/HelpDoc"
import type { Span } from "@effect/cli/HelpDoc/Span"
import * as internal from "@effect/cli/internal/cliApp"
Expand All @@ -23,17 +22,6 @@ export interface CliApp<A> {
readonly footer: HelpDoc
}

/**
* @since 1.0.0
*/
export declare namespace CliApp {
/**
* @since 1.0.0
* @category models
*/
export type Context = Console
}

/**
* @since 1.0.0
* @category constructors
Expand All @@ -55,11 +43,11 @@ export const make: <A>(
export const run: {
<R, E, A>(
args: ReadonlyArray<string>,
f: (a: A) => Effect<CliApp.Context | R, E, void>
): (self: CliApp<A>) => Effect<CliApp.Context | R, E | ValidationError, void>
f: (a: A) => Effect<R, E, void>
): (self: CliApp<A>) => Effect<R, E | ValidationError, void>
<R, E, A>(
self: CliApp<A>,
args: ReadonlyArray<string>,
f: (a: A) => Effect<CliApp.Context | R, E, void>
): Effect<CliApp.Context | R, ValidationError | E, void>
f: (a: A) => Effect<R, E, void>
): Effect<R, ValidationError | E, void>
} = internal.run
44 changes: 0 additions & 44 deletions src/Console.ts

This file was deleted.

84 changes: 39 additions & 45 deletions src/internal/cliApp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type * as BuiltInOption from "@effect/cli/BuiltInOption"
import type * as CliApp from "@effect/cli/CliApp"
import type * as Command from "@effect/cli/Command"
import * as Console from "@effect/cli/Console"
import type * as HelpDoc from "@effect/cli/HelpDoc"
import type * as Span from "@effect/cli/HelpDoc/Span"
import * as cliConfig from "@effect/cli/internal/cliConfig"
Expand All @@ -15,6 +14,7 @@ import type * as ValidationError from "@effect/cli/ValidationError"
import * as Context from "@effect/data/Context"
import { dual, pipe } from "@effect/data/Function"
import * as Option from "@effect/data/Option"
import * as Console from "@effect/io/Console"
import * as Effect from "@effect/io/Effect"

const defaultConfig = {
Expand All @@ -35,13 +35,13 @@ export const make = <A>(config: {
export const run = dual<
<R, E, A>(
args: ReadonlyArray<string>,
f: (a: A) => Effect.Effect<R | CliApp.CliApp.Context, E, void>
) => (self: CliApp.CliApp<A>) => Effect.Effect<R | CliApp.CliApp.Context, E | ValidationError.ValidationError, void>,
f: (a: A) => Effect.Effect<R, E, void>
) => (self: CliApp.CliApp<A>) => Effect.Effect<R, E | ValidationError.ValidationError, void>,
<R, E, A>(
self: CliApp.CliApp<A>,
args: ReadonlyArray<string>,
f: (a: A) => Effect.Effect<R | CliApp.CliApp.Context, E, void>
) => Effect.Effect<R | CliApp.CliApp.Context, E | ValidationError.ValidationError, void>
f: (a: A) => Effect.Effect<R, E, void>
) => Effect.Effect<R, E | ValidationError.ValidationError, void>
>(3, (self, args, f) =>
Effect.contextWithEffect((context: Context.Context<never>) => {
const config = Option.getOrElse(Context.getOption(context, cliConfig.Tag), () => cliConfig.defaultConfig)
Expand Down Expand Up @@ -79,33 +79,29 @@ const runBuiltInMap: {
[K in BuiltInOption.BuiltInOption["_tag"]]: (
self: Extract<BuiltInOption.BuiltInOption, { _tag: K }>,
cliApp: CliApp.CliApp<any>
) => Effect.Effect<CliApp.CliApp.Context, never, void>
) => Effect.Effect<never, never, void>
} = {
ShowCompletions: () =>
Effect.sync(() => {
// case ShowCompletions(index, _) =>
// envs.flatMap { envMap =>
// val compWords = envMap.collect {
// case (idx, word) if idx.startsWith("COMP_WORD_") =>
// (idx.drop("COMP_WORD_".length).toInt, word)
// }.toList.sortBy(_._1).map(_._2)
// case ShowCompletions(index, _) =>
// envs.flatMap { envMap =>
// val compWords = envMap.collect {
// case (idx, word) if idx.startsWith("COMP_WORD_") =>
// (idx.drop("COMP_WORD_".length).toInt, word)
// }.toList.sortBy(_._1).map(_._2)

// Completion
// .complete(compWords, index, self.command, self.config)
// .flatMap { completions =>
// ZIO.foreachDiscard(completions)(word => printLine(word))
// }
// }
console.log("Showing Completions")
}),
// Completion
// .complete(compWords, index, self.command, self.config)
// .flatMap { completions =>
// ZIO.foreachDiscard(completions)(word => printLine(word))
// }
// }
Console.log("Showing Completions"),
ShowCompletionScript: () =>
Effect.sync(() => {
// case ShowCompletionScript(path, shellType) =>
// printLine(
// CompletionScript(path, if (self.command.names.nonEmpty) self.command.names else Set(self.name), shellType)
// )
console.log("Showing Completion Script")
}),
// case ShowCompletionScript(path, shellType) =>
// printLine(
// CompletionScript(path, if (self.command.names.nonEmpty) self.command.names else Set(self.name), shellType)
// )
Console.log("Showing Completion Script"),
ShowHelp: (self, cliApp) => {
const banner = doc.h1(span.code(cliApp.name))
const header = doc.p(span.concat(span.text(`${cliApp.name} v${cliApp.version} -- `), cliApp.summary))
Expand All @@ -125,27 +121,25 @@ const runBuiltInMap: {
return Console.log(helpText)
},
Wizard: () =>
Effect.sync(() => {
// val subcommands = command.getSubcommands
// for {
// subcommandName <- if (subcommands.size == 1) ZIO.succeed(subcommands.keys.head)
// else
// (print("Command" + subcommands.keys.mkString("(", "|", "): ")) *> readLine).orDie
// subcommand <-
// ZIO
// .fromOption(subcommands.get(subcommandName))
// .orElseFail(ValidationError(ValidationErrorType.InvalidValue, HelpDoc.p("Invalid subcommand")))
// args <- subcommand.generateArgs
// _ <- Console.printLine(s"Executing command: ${(prefix(self.command) ++ args).mkString(" ")}")
// result <- self.run(args)
// } yield result
console.log("Running Wizard")
})
// val subcommands = command.getSubcommands
// for {
// subcommandName <- if (subcommands.size == 1) ZIO.succeed(subcommands.keys.head)
// else
// (print("Command" + subcommands.keys.mkString("(", "|", "): ")) *> readLine).orDie
// subcommand <-
// ZIO
// .fromOption(subcommands.get(subcommandName))
// .orElseFail(ValidationError(ValidationErrorType.InvalidValue, HelpDoc.p("Invalid subcommand")))
// args <- subcommand.generateArgs
// _ <- Console.printLine(s"Executing command: ${(prefix(self.command) ++ args).mkString(" ")}")
// result <- self.run(args)
// } yield result
Console.log("Running Wizard")
}

const runBuiltIn = <A>(
self: BuiltInOption.BuiltInOption,
cliApp: CliApp.CliApp<A>
): Effect.Effect<CliApp.CliApp.Context, never, void> => runBuiltInMap[self._tag](self as any, cliApp)
): Effect.Effect<never, never, void> => runBuiltInMap[self._tag](self as any, cliApp)

const printDocs = (error: ValidationError.ValidationError) => Console.log(doc.toAnsiText(error.error))

0 comments on commit 22dd35f

Please sign in to comment.