Skip to content

Commit

Permalink
cli module based on github.com/urfave/cli (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
myzie authored Feb 13, 2024
1 parent dfa7ec9 commit b1d3a53
Show file tree
Hide file tree
Showing 21 changed files with 1,290 additions and 15 deletions.
2 changes: 2 additions & 0 deletions cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/risor-io/risor/importer"
modBase64 "github.com/risor-io/risor/modules/base64"
modBytes "github.com/risor-io/risor/modules/bytes"
modCli "github.com/risor-io/risor/modules/cli"
modDns "github.com/risor-io/risor/modules/dns"
modExec "github.com/risor-io/risor/modules/exec"
modFilepath "github.com/risor-io/risor/modules/filepath"
Expand Down Expand Up @@ -97,6 +98,7 @@ func (cfg *Config) addDefaultGlobals() {
modules := map[string]object.Object{
"base64": modBase64.Module(),
"bytes": modBytes.Module(),
"cli": modCli.Module(),
"exec": modExec.Module(),
"filepath": modFilepath.Module(),
"fmt": modFmt.Module(),
Expand Down
8 changes: 7 additions & 1 deletion cmd/risor/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/risor-io/risor/cmd/risor

go 1.21
go 1.21.6

replace (
github.com/risor-io/risor => ../..
github.com/risor-io/risor/modules/aws => ../../modules/aws
github.com/risor-io/risor/modules/cli => ../../modules/cli
github.com/risor-io/risor/modules/gha => ../../modules/gha
github.com/risor-io/risor/modules/image => ../../modules/image
github.com/risor-io/risor/modules/jmespath => ../../modules/jmespath
Expand Down Expand Up @@ -98,6 +99,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/xray v1.18.0 // indirect
github.com/aws/smithy-go v1.14.2 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
Expand Down Expand Up @@ -145,14 +147,18 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/risor-io/risor/modules/cli v0.0.0-00010101000000-000000000000 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/xo/dburl v0.20.0 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230314191032-db074128a8ec // indirect
golang.org/x/image v0.14.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions cmd/risor/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -463,6 +465,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
Expand Down Expand Up @@ -506,11 +509,15 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xo/dburl v0.20.0 h1:v601OhM9J4Zh56R270ncM9HRgoxp39tf9+nt5ft9UD0=
github.com/xo/dburl v0.20.0/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
21 changes: 17 additions & 4 deletions cmd/risor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func fatal(msg string, args ...interface{}) {
os.Exit(1)
}

func isTerminalIO() bool {
stdin := os.Stdin.Fd()
stdout := os.Stdout.Fd()
inTerm := isatty.IsTerminal(stdin) || isatty.IsCygwinTerminal(stdin)
outTerm := isatty.IsTerminal(stdout) || isatty.IsCygwinTerminal(stdout)
return inTerm && outTerm
}

// this works around issues with cobra not passing args after -- to the script
// see: https://github.com/spf13/cobra/issues/1877
// passedargs is everything after the '--'
Expand Down Expand Up @@ -272,10 +280,9 @@ var rootCmd = &cobra.Command{
if len(args) > 0 && codeWasSupplied {
fatal(red("cannot specify both code and a filepath"))
}
if len(args) == 0 && !codeWasSupplied && !viper.GetBool("stdin") {
stdoutFd := os.Stdout.Fd()
if !isatty.IsTerminal(stdoutFd) && !isatty.IsCygwinTerminal(stdoutFd) {
fatal("cannot show repl: stdout is not a terminal")
if len(args) == 0 && !codeWasSupplied && !viper.GetBool("stdin") && len(passedargs) == 0 {
if !isTerminalIO() {
fatal("cannot show repl: stdin or stdout is not a terminal")
}
if err := repl.Run(ctx, opts); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", red(err.Error()))
Expand All @@ -298,6 +305,12 @@ var rootCmd = &cobra.Command{
fatal(red(err.Error()))
}
code = string(bytes)
} else if len(passedargs) > 0 {
bytes, err := os.ReadFile(passedargs[0])
if err != nil {
fatal(red(err.Error()))
}
code = string(bytes)
}

start := time.Now()
Expand Down
71 changes: 71 additions & 0 deletions examples/scripts/encode_cli.risor
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env risor --

from cli import app, command, flag

// Usage:
// ./examples/scripts/encode_cli.risor base64 hello
// aGVsbG8=
//
// ./examples/scripts/encode_cli.risor hex hello
// 68656c6c6f
//
// ./examples/scripts/encode_cli.risor -h
// <help text shown>
//
// ./examples/scripts/encode_cli.risor list-codecs -v
// Codec 1: base64
// Codec 2: hex

// Create a new CLI app named "encode"
app({
name: "encode",
help_name: "encode",
description: "Encode input text using various codecs",
// Register three commands, named "base64", "hex", and "list-codecs"
commands: [
command({
name: "base64",
action: func(ctx) {
// Print the base64 encoded text
for _, text := range ctx.args() {
print(encode(text, "base64"))
}
}
}),
command({
name: "hex",
action: func(ctx) {
// Print the hex encoded text
for _, text := range ctx.args() {
print(encode(text, "hex"))
}
}
}),
command({
name: "list-codecs",
flags: [
flag({
name: "verbose",
aliases: ["v"],
usage: "Print verbose output",
env_vars: ["VERBOSE"],
value: false,
}),
],
action: func(ctx) {
codecs := ["base64", "hex"]
if ctx.bool("verbose") {
// Print all available codecs
for i, codec := range codecs {
print('Codec {i+1}: {codec}')
}
} else {
// Print only the codec names
for _, codec := range codecs {
print(codec)
}
}
}
}),
]
}).run() // Run the app
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.21
go 1.21.6

use (
.
Expand All @@ -9,6 +9,7 @@ use (
./cmd/risor-modgen
./examples/go/struct
./modules/aws
./modules/cli
./modules/gha
./modules/image
./modules/jmespath
Expand Down
3 changes: 2 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ cloud.google.com/go/vpcaccess v1.6.0/go.mod h1:wX2ILaNhe7TlVa4vC5xce1bCnqE3AeH27
cloud.google.com/go/webrisk v1.8.0/go.mod h1:oJPDuamzHXgUc+b8SiHRcVInZQuybnvEW72PqTc7sSg=
cloud.google.com/go/websecurityscanner v1.5.0/go.mod h1:Y6xdCPy81yi0SQnDY1xdNTNpfY1oAgXUlcfN3B3eSng=
cloud.google.com/go/workflows v1.10.0/go.mod h1:fZ8LmRmZQWacon9UCX1r/g/DfAXx5VcPALq2CxzdePw=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
Expand All @@ -150,6 +151,7 @@ github.com/cncf/xds/go v0.0.0-20230310173818-32f1caf87195/go.mod h1:eXthEFrGJvWH
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c=
github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
Expand Down Expand Up @@ -208,7 +210,6 @@ github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFu
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/sagikazarmark/crypt v0.10.0/go.mod h1:gwTNHQVoOS3xp9Xvz5LLR+1AauC5M6880z5NWzdhOyQ=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
Expand Down
Loading

0 comments on commit b1d3a53

Please sign in to comment.