-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2242 from turkmenkaan/f/add-play-cmd
cli: add play cmd
- Loading branch information
Showing
5 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package d2cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"oss.terrastruct.com/d2/lib/urlenc" | ||
"oss.terrastruct.com/util-go/xbrowser" | ||
"oss.terrastruct.com/util-go/xmain" | ||
) | ||
|
||
func playCmd(ctx context.Context, ms *xmain.State) error { | ||
if len(ms.Opts.Flags.Args()) != 2 { | ||
return xmain.UsageErrorf("play must be passed one argument: either a filepath or '-' for 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 := readInput(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encoded, err := urlenc.Encode(fileRaw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url := fmt.Sprintf("https://play.d2lang.com/?script=%s&sketch=%d&theme=%d&", encoded, sketchNumber, theme) | ||
openBrowser(ctx, ms, url) | ||
return nil | ||
} | ||
|
||
func readInput(filepath string) (string, error) { | ||
if filepath == "-" { | ||
data, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
return "", fmt.Errorf("error reading from stdin: %w", err) | ||
} | ||
return string(data), nil | ||
} | ||
|
||
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) | ||
} | ||
} |