Skip to content

Commit dd53a18

Browse files
author
Florian Heinze
committed
TASK: improved UX
1 parent f1c56a2 commit dd53a18

11 files changed

Lines changed: 120 additions & 14 deletions

File tree

cmd/init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111

1212
func buildInitCommand() *cobra.Command {
1313
return &cobra.Command{
14+
// IMPORTANT: never color Use! You will not be able to run the command otherwise.
1415
Use: "DSR_INIT",
15-
Short: "creates dev.sh and dev_setup.sh in current folder",
16+
Short: color.Gray.Text("creates dev.sh and dev_setup.sh in current folder"),
1617
Long: color.Sprintf(`This creates an example create a dev.sh and dev_setup.sh in your current directory.`),
1718
Args: cobra.NoArgs,
1819

cmd/no_tasks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package cmd
22

33
import (
4+
"github.com/gookit/color"
45
"github.com/spf13/cobra"
56
)
67

78
// This is a hack add a hint to cobra rendering
89
func buildNoTaskCommand(reason string) *cobra.Command {
910
return &cobra.Command{
10-
Short: "\n " + reason + "\n",
11+
Short: color.Magenta.Text("\n " + reason + "\n"),
1112
DisableFlagParsing: true,
1213
DisableAutoGenTag: true,
1314
DisableFlagsInUseLine: true,

cmd/root.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"github.com/gookit/color"
56
"github.com/spf13/cobra"
67
"log"
78
"main/utils"
@@ -24,12 +25,14 @@ and other nifty feature ;)
2425
// Execute adds all child commands to the root command and sets flags appropriately.
2526
// This is called by main.main(). It only needs to happen once to the rootCmd.
2627
func Execute(version, commit string) {
28+
// IMPORTANT: the order of tasks should resemble the order in the dev.sh
2729
cobra.EnableCommandSorting = false
28-
RootCmd.AddCommand(buildSectionCommand("tasks"))
30+
RootCmd.AddCommand(buildSectionCommand("your tasks"))
2931
addDevScriptTasksAsCommands(RootCmd)
3032
RootCmd.AddCommand(buildSectionCommand("utils"))
3133
RootCmd.AddCommand(buildInitCommand())
3234
// RootCmd.AddCommand(buildCompletionCommand())
35+
RootCmd.AddCommand(buildSectionCommand("other"))
3336
if err := RootCmd.Execute(); err != nil {
3437
fmt.Println(err)
3538
os.Exit(1)
@@ -52,7 +55,10 @@ func addDevScriptTasksAsCommands(rootCmd *cobra.Command) {
5255
if utils.FileExists(devScriptPath) {
5356
if utils.FileContains(devScriptPath, utils.DEV_SCRIPT_MARKER) {
5457
tasks := utils.ParseDevScriptTasks(devScriptPath)
55-
rootCmd.Long = rootCmd.Long + "\nDEV SCRIPT WITH " + strconv.Itoa(len(tasks)) + " TASKS FOUND AT:\n " + devScriptPath
58+
rootCmd.Long = rootCmd.Long + color.Magenta.Text("\nDEV SCRIPT WITH ")
59+
rootCmd.Long = rootCmd.Long + color.Bold.Text(strconv.Itoa(len(tasks)))
60+
rootCmd.Long = rootCmd.Long + color.Magenta.Text(" TASKS FOUND AT:\n ")
61+
rootCmd.Long = rootCmd.Long + color.Magenta.Text(devScriptPath)
5662
if len(tasks) > 0 {
5763
for _, task := range tasks {
5864
rootCmd.AddCommand(buildTaskCommand(task, devScriptPath))

cmd/section.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/gookit/color"
45
"github.com/spf13/cobra"
56
"main/utils"
67
)
@@ -15,7 +16,7 @@ func buildSectionTitle(title string) string {
1516

1617
func buildSectionCommand(title string) *cobra.Command {
1718
return &cobra.Command{
18-
Short: buildSectionTitle(title),
19+
Short: color.Gray.Text(buildSectionTitle(title)),
1920
DisableFlagParsing: true,
2021
DisableAutoGenTag: true,
2122
DisableFlagsInUseLine: true,

cmd/task.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/gookit/color"
45
"github.com/spf13/cobra"
56
"log"
67
"main/utils"
@@ -11,12 +12,13 @@ import (
1112

1213
func buildTaskCommand(task utils.DevScriptTask, devScriptPath string) *cobra.Command {
1314
var cmd = &cobra.Command{
14-
Use: task.Name,
15-
Short: task.Comments,
16-
Long: task.Comments,
15+
// IMPORTANT: never color Use! You will not be able to run the command otherwise.
16+
Use: task.Usage,
17+
Short: color.Gray.Text(task.Short),
18+
Long: task.Long,
1719
Args: cobra.ArbitraryArgs,
1820
Run: func(cmd *cobra.Command, args []string) {
19-
execDevScriptWithArguments(devScriptPath, append([]string{task.Name}, args...))
21+
execDevScriptWithArguments(devScriptPath, append([]string{task.Usage}, args...))
2022
},
2123
}
2224
return cmd

dev.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ set -e
1111

1212
######### TASKS #########
1313

14+
# runs foo
1415
function foo() {
1516
echo "test"
1617
}
1718

19+
# use main aus binary
1820
function switch-dev-binary() {
1921
echo "-----------> creating build"
2022
build
@@ -27,6 +29,7 @@ function switch-dev-binary() {
2729
mv ./main /usr/local/bin/dev || true
2830
}
2931

32+
# restores original dev binary
3033
function restore-dev-binary() {
3134
if test -f "/usr/local/bin/dev_back"; then
3235
echo "/usr/local/bin/dev_back exists."

test/dev.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
# This script is used to document and run recurring tasks in development. #
4+
# #
5+
# You can run your tasks using the script `./dev some-task`. #
6+
# You can install the Sandstorm Dev Script Runner and run your tasks from any #
7+
# nested folder using `dev some-task`. #
8+
# https://github.com/sandstorm/Sandstorm.DevScriptRunner #
9+
###############################################################################
10+
11+
set -e
12+
13+
######### TASKS #########
14+
15+
# easy setup of the project
16+
function setup() {
17+
# As the setup typically is more complex we recommend using a separate shell script
18+
./dev_setup.sh
19+
}
20+
21+
# sometask to help with something
22+
#
23+
# The first line of the comment will we used in the task overview.
24+
# If you want to provide more details just add more lines ;)
25+
function sometask() {
26+
# Most task will only require some steps. We recommend implementing them here
27+
_log_success "Some task"
28+
_log_warning "TODO: implement more steps"
29+
}
30+
31+
# another task to help with something else
32+
#
33+
# The first line of the comment will we used in the task overview.
34+
# If you want to provide more details just add more lines ;)
35+
function taskwitharguments() {
36+
# You can access arguments using $@ array. The task name will not be part of the array
37+
_log_success "Task with arguments"
38+
_log_warning "TODO: implement more steps"
39+
_log_success "Arguments"
40+
_log_success ' $0: '"$0"
41+
_log_success ' $1: '"$1"
42+
_log_success ' $2: '"$2"
43+
}
44+
45+
####### Utilities #######
46+
47+
_log_success() {
48+
printf "\033[0;32m%s\033[0m\n" "${1}"
49+
}
50+
_log_warning() {
51+
printf "\033[1;33m%s\033[0m\n" "${1}"
52+
}
53+
_log_error() {
54+
printf "\033[0;31m%s\033[0m\n" "${1}"
55+
}
56+
57+
# THIS NEEDS TO BE LAST!!!
58+
# this will run your tasks
59+
"$@"

test/dev_setup.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# For more complex tasks it is useful to create a separate shell script,
4+
# e.g. for the setup you can provide a great first run experience ;)
5+
6+
set -e
7+
8+
echo "TODO: implement ./dev_setup.sh"

utils/DevScriptTask.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
type DevScriptTask struct {
4-
Name string
5-
Comments string
4+
Usage string
5+
Short string
6+
Long string
67
}

utils/templates/dev.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@ set -e
1212

1313
######### TASKS #########
1414

15+
# easy setup of the project
1516
function setup() {
1617
# As the setup typically is more complex we recommend using a separate shell script
1718
./dev_setup.sh
1819
}
1920

21+
# sometask to help with something
22+
#
23+
# The first line of the comment will we used in the task overview.
24+
# If you want to provide more details just add more lines ;)
2025
function sometask() {
2126
# Most task will only require some steps. We recommend implementing them here
2227
_log_success "Some task"
2328
_log_warning "TODO: implement more steps"
2429
}
2530

31+
# another task to help with something else
32+
#
33+
# The first line of the comment will we used in the task overview.
34+
# If you want to provide more details just add more lines ;)
2635
function taskwitharguments() {
2736
# You can access arguments using $@ array. The task name will not be part of the array
2837
_log_success "Task with arguments"

0 commit comments

Comments
 (0)