Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 8b4841d

Browse files
committed
added function management
1 parent 5c5c60d commit 8b4841d

File tree

6 files changed

+477
-0
lines changed

6 files changed

+477
-0
lines changed

cmd/function.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// funcitonCmd operates on server-side functions
10+
var functionCmd = &cobra.Command{
11+
Use: "function",
12+
Short: "Manage your server-side functions.",
13+
Long: fmt.Sprintf(`
14+
%s
15+
16+
You can create, update, list and view run history for all your functions.
17+
18+
You'll need a rootToken in your
19+
config file.
20+
`,
21+
clbold(clsecondary("Manage functions")),
22+
),
23+
Run: func(cmd *cobra.Command, args []string) {
24+
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(functionCmd)
30+
31+
// Here you will define your flags and configuration settings.
32+
33+
// Cobra supports Persistent Flags which will work for this command
34+
// and all subcommands, e.g.:
35+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
36+
37+
// Cobra supports local flags which will only run when this command
38+
// is called directly, e.g.:
39+
// accountCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
40+
}

cmd/functionCreate.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/staticbackendhq/backend-go"
9+
)
10+
11+
// functionCreate creates a new server-side function
12+
var functionCreateCmd = &cobra.Command{
13+
Use: "add",
14+
Short: "Create a new server-side function",
15+
Long: fmt.Sprintf(`
16+
%s
17+
18+
Create a function that executes on a recurring schedule:
19+
20+
backend funciton add --name trial_expire --trigger daily_task_trial_expire --source ./functions/trial_expire.js
21+
22+
You may create server-side functions that execute based on those triggers:
23+
24+
%s: invoke via a URL
25+
%s: a topic/event that will run your function when published
26+
`,
27+
clbold(clsecondary("Create a function")),
28+
clbold("web"),
29+
clbold("any_topic_here"),
30+
),
31+
Run: func(cmd *cobra.Command, args []string) {
32+
if setBackend() == false {
33+
return
34+
}
35+
36+
tok, ok := getRootToken()
37+
if !ok {
38+
return
39+
}
40+
41+
name, err := cmd.Flags().GetString("name")
42+
if err != nil || len(name) == 0 {
43+
fmt.Printf("%s: the --name option is required\n", cldanger("missing parameter"))
44+
return
45+
}
46+
47+
trigger, err := cmd.Flags().GetString("trigger")
48+
if err != nil || len(trigger) == 0 {
49+
fmt.Printf("%s: the --trigger option is required\n", cldanger("missing parameter"))
50+
return
51+
}
52+
53+
source, err := cmd.Flags().GetString("source")
54+
if err != nil || len(trigger) == 0 {
55+
fmt.Printf("%s: the --source option is required\n", cldanger("missing parameter"))
56+
return
57+
}
58+
59+
b, err := os.ReadFile(source)
60+
if err != nil {
61+
fmt.Printf("%s: %v\n", cldanger("error reading source file"), err)
62+
return
63+
}
64+
65+
fn := backend.Function{
66+
FunctionName: name,
67+
TriggerTopic: trigger,
68+
Code: string(b),
69+
}
70+
71+
if err := backend.AddFunction(tok, fn); err != nil {
72+
fmt.Printf("%s: %v\n", cldanger("error adding your function"), err)
73+
return
74+
}
75+
76+
fmt.Println("%s: %s", clgreen("Function created successfully"), clbold(name))
77+
if trigger == "web" {
78+
fmt.Printf("%s: %s\n", clsecondary("Function URL"), clbold("[your_domain]/fn/exec/"+name))
79+
} else {
80+
fmt.Printf("%s: %s\n", clsecondary("Function will trigger on topic"), clbold(trigger))
81+
}
82+
},
83+
}
84+
85+
func init() {
86+
functionCmd.AddCommand(functionCreateCmd)
87+
88+
// Here you will define your flags and configuration settings.
89+
90+
// Cobra supports Persistent Flags which will work for this command
91+
// and all subcommands, e.g.:
92+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
93+
94+
functionCreateCmd.Flags().String("name", "", "function name")
95+
functionCreateCmd.Flags().String("trigger", "", "execution trigger either web or topic")
96+
functionCreateCmd.Flags().String("source", "", "path of the JavaScript file")
97+
}

cmd/functionDelete.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/staticbackendhq/backend-go"
8+
)
9+
10+
// functionList lists all server-side functions
11+
var functionDeleteCmd = &cobra.Command{
12+
Use: "delete name",
13+
Short: "Delete the function by its name",
14+
Long: fmt.Sprintf(`
15+
%s
16+
17+
This will delete the function permanently, including all of its run history.
18+
`,
19+
clbold(clsecondary("Delete a functions")),
20+
),
21+
Run: func(cmd *cobra.Command, args []string) {
22+
if setBackend() == false {
23+
return
24+
}
25+
26+
tok, ok := getRootToken()
27+
if !ok {
28+
return
29+
}
30+
31+
if len(args) != 1 {
32+
fmt.Printf("%s: only a name should be specified\n", cldanger("argument missmatch"))
33+
return
34+
}
35+
36+
if err := backend.DeleteFunction(tok, args[0]); err != nil {
37+
fmt.Printf("%s: %v\n", cldanger("error deleting your function"), err)
38+
return
39+
}
40+
41+
fmt.Printf("%s: the function %s has been deleted\n", clsuccess("success"))
42+
},
43+
}
44+
45+
func init() {
46+
functionCmd.AddCommand(functionDeleteCmd)
47+
48+
// Here you will define your flags and configuration settings.
49+
50+
// Cobra supports Persistent Flags which will work for this command
51+
// and all subcommands, e.g.:
52+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
53+
}

cmd/functionInfo.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"text/tabwriter"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/staticbackendhq/backend-go"
10+
)
11+
12+
// functionInfo returns all run history for a specific function
13+
var functionInfoCmd = &cobra.Command{
14+
Use: "info name",
15+
Short: "Display run history for a function",
16+
Long: fmt.Sprintf(`
17+
%s
18+
19+
This command displays the last 100 run history and function output.
20+
21+
This is useful to diagnose if you're using the %s runtime function inside your
22+
function code.
23+
`,
24+
clbold(clsecondary("Display function run hostory")),
25+
clbold("log"),
26+
),
27+
Run: func(cmd *cobra.Command, args []string) {
28+
if setBackend() == false {
29+
return
30+
}
31+
32+
tok, ok := getRootToken()
33+
if !ok {
34+
return
35+
}
36+
37+
if len(args) != 1 {
38+
fmt.Printf("%s: only a name should be specified\n", cldanger("argument missmatch"))
39+
return
40+
}
41+
42+
fn, err := backend.FunctionInfo(tok, args[0])
43+
if err != nil {
44+
fmt.Printf("%s: %v\n", cldanger("error while retrieving the function"), err)
45+
return
46+
}
47+
48+
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', tabwriter.DiscardEmptyColumns)
49+
50+
fmt.Fprintf(w, "NAME\tVERSION\tTRIGGER\tLAST RUN\n")
51+
52+
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n",
53+
fn.FunctionName,
54+
fn.Version,
55+
fn.TriggerTopic,
56+
fn.LastRun.Format("2006/01/02 15:04"),
57+
)
58+
59+
w.Flush()
60+
61+
fmt.Printf("\n==== %s ====\n\n", clbold("RUN HISTORY"))
62+
63+
start := len(fn.History)
64+
end := start - 100
65+
if end < 0 {
66+
end = 0
67+
}
68+
if start > 0 {
69+
for i := start; i >= end; i-- {
70+
run := fn.History[i]
71+
fmt.Printf("%s:%d | %s:%s | %s:%v\n",
72+
clsecondary("version"),
73+
run.Version,
74+
clsecondary("start"),
75+
run.Started.Format("2006/01/02 15:04"),
76+
clsecondary("execution time"),
77+
run.Completed.Sub(run.Started),
78+
)
79+
for _, o := range run.Output {
80+
fmt.Println("\t", o)
81+
}
82+
83+
fmt.Println("------------------")
84+
}
85+
}
86+
},
87+
}
88+
89+
func init() {
90+
functionCmd.AddCommand(functionInfoCmd)
91+
92+
// Here you will define your flags and configuration settings.
93+
94+
// Cobra supports Persistent Flags which will work for this command
95+
// and all subcommands, e.g.:
96+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
97+
}

cmd/functionList.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"text/tabwriter"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/staticbackendhq/backend-go"
11+
)
12+
13+
// functionList lists all server-side functions
14+
var functionListCmd = &cobra.Command{
15+
Use: "list [trigger]",
16+
Short: "List server-side functions",
17+
Long: fmt.Sprintf(`
18+
%s
19+
20+
If you specify a %s only functions for that trigger will be included.
21+
22+
Otherwise, all functions are displayed.
23+
`,
24+
clbold(clsecondary("List functions")),
25+
clbold("trigger"),
26+
),
27+
Run: func(cmd *cobra.Command, args []string) {
28+
if setBackend() == false {
29+
return
30+
}
31+
32+
tok, ok := getRootToken()
33+
if !ok {
34+
return
35+
}
36+
37+
var trigger string
38+
if len(args) == 1 {
39+
trigger = args[0]
40+
}
41+
42+
results, err := backend.ListFunctions(tok)
43+
if err != nil {
44+
fmt.Printf("%s: %v\n", cldanger("An error occured"), err)
45+
return
46+
}
47+
48+
// filter for trigger if supplied
49+
var filtered []backend.Function
50+
if len(trigger) > 0 {
51+
for _, f := range results {
52+
if strings.EqualFold(trigger, f.TriggerTopic) {
53+
filtered = append(filtered, f)
54+
}
55+
}
56+
} else {
57+
filtered = append(filtered, results...)
58+
}
59+
60+
fmt.Printf("%s result(s)\n\n", clbold(len(filtered)))
61+
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', tabwriter.DiscardEmptyColumns)
62+
63+
fmt.Fprintf(w, "NAME\tVERSION\tTRIGGER\tLAST RUN\n")
64+
65+
for _, f := range filtered {
66+
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n",
67+
f.FunctionName,
68+
f.Version,
69+
f.TriggerTopic,
70+
f.LastRun.Format("2006/01/02 15:04"),
71+
)
72+
}
73+
74+
w.Flush()
75+
},
76+
}
77+
78+
func init() {
79+
functionCmd.AddCommand(functionListCmd)
80+
81+
// Here you will define your flags and configuration settings.
82+
83+
// Cobra supports Persistent Flags which will work for this command
84+
// and all subcommands, e.g.:
85+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
86+
}

0 commit comments

Comments
 (0)