-
Notifications
You must be signed in to change notification settings - Fork 474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cli: add play cmd #2242
base: master
Are you sure you want to change the base?
cli: add play cmd #2242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { | |
return nil | ||
case "fmt": | ||
return fmtCmd(ctx, ms) | ||
case "play": | ||
return playSubcommand(ctx, ms) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
case "version": | ||
if len(ms.Opts.Flags.Args()) > 1 { | ||
return xmain.UsageErrorf("version subcommand accepts no arguments") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package d2cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"oss.terrastruct.com/d2/lib/urlenc" | ||
"oss.terrastruct.com/util-go/xbrowser" | ||
"oss.terrastruct.com/util-go/xmain" | ||
) | ||
|
||
func playSubcommand(ctx context.Context, ms *xmain.State) error { | ||
if len(ms.Opts.Flags.Args()) != 2 { | ||
return xmain.UsageErrorf("play must be passed one file to open") | ||
} | ||
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should also allow stdin |
||
filepath := ms.Opts.Flags.Args()[1] | ||
|
||
theme, err := ms.Opts.Flags.GetInt64("theme") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sketch, err := ms.Opts.Flags.GetBool("sketch") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var sketchNumber int | ||
if sketch { | ||
sketchNumber = 1 | ||
} else { | ||
sketchNumber = 0 | ||
} | ||
|
||
fileRaw, err := readFile(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encoded, err := urlenc.Encode(fileRaw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url := fmt.Sprintf("https://play.d2lang.com/?l=&script=%s&sketch=%d&theme=%d&", encoded, sketchNumber, theme) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was in the URL provided in the issue. I'll remove. |
||
openBrowser(ctx, ms, url) | ||
alixander marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
|
||
func readFile(filepath string) (string, error) { | ||
data, err := os.ReadFile(filepath) | ||
if err != nil { | ||
return "", xmain.UsageErrorf(err.Error()) | ||
} | ||
|
||
return string(data), nil | ||
} | ||
|
||
func openBrowser(ctx context.Context, ms *xmain.State, url string) { | ||
ms.Log.Info.Printf("opening playground: %s", url) | ||
|
||
err := xbrowser.Open(ctx, ms.Env, url) | ||
if err != nil { | ||
ms.Log.Warn.Printf("failed to open browser to %v: %v", url, err) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
playground
might be unknown to folks. I'd add a description likeOpens the file in the d2 playground, an online web viewer