/
run.go
94 lines (81 loc) · 2.38 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"github.com/runner-x/runner-x/cli/runner/client"
"github.com/runner-x/runner-x/engine/coderunner"
"github.com/runner-x/runner-x/server/api"
"github.com/spf13/cobra"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "runs the supplied code loaded in from a file.",
Run: func(cmd *cobra.Command, args []string) {
//TODO: implement flag logic, implement source file args parsing
argLen := len(args)
if argLen < 1 {
fmt.Println("No file specified for compilation; Please specify a file!")
return
} else if argLen > 1 {
fmt.Println("Multiple file compilation not yet supported. Please only specify a single file for compilation.")
return
}
str, err := cmd.Flags().GetString("lang")
if err != nil {
panic(err)
}
filename := args[0]
ext := extractExtension(filename)
var langCheck coderunner.Language
if str == "implicit" {
if lang, found := coderunner.ExtensionFileMap[ext]; found {
langCheck = lang
} else {
fmt.Printf("unrecognized file type: %s", ext)
return
}
}
source, err := os.ReadFile(filename)
if err != nil {
fmt.Printf("File not found: %s", filename)
return
}
var cmdClient client.Requester = client.NewClient()
r := &api.RunRequest{
Source: string(source[:]),
Lang: langCheck,
}
resp, err := cmdClient.Run(r)
if err != nil {
fmt.Println(err)
} else if resp.Error != nil {
fmt.Println(resp.Error)
} else {
fmt.Printf("Stdout: %s\nStderr: %s\n", resp.Stdout, resp.Stderr)
}
},
}
func init() {
rootCmd.AddCommand(runCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// runCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
runCmd.Flags().StringP("lang", "l", "implicit", "specifies the language for the source file. If not specified, CLI will try to guess language type before making API call.")
}
func extractExtension(filename string) string {
f := []rune(filename)
for i := len(f) - 1; i >= 0; i -= 1 {
if f[i] == '.' {
return string(f[i+1:])
}
}
return filename
}