Skip to content

Commit aea4589

Browse files
Merge pull request #21 from camerondurham/dev/arek
Dev/arek
2 parents 1360aa3 + ec60b66 commit aea4589

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

cli/runner/cmd/langs.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13+
const (
14+
LANG_ENDPOINT = "/api/v1/languages"
15+
)
16+
17+
var (
18+
server string = "http://localhost:8080"
19+
)
20+
1321
// langsCmd represents the langs command
1422
var langsCmd = &cobra.Command{
1523
Use: "langs",
1624
Short: "query the server for supported languages",
1725
Run: func(cmd *cobra.Command, args []string) {
1826
// implement CLI subcommand logic here
19-
fmt.Println("langs called")
27+
resp, err := getLangListJSON(server, LANG_ENDPOINT)
28+
if err.Error() != "" {
29+
fmt.Println(err)
30+
return
31+
}
32+
fmt.Print("Currently supported languages: ")
33+
for _, lang := range resp.Languages {
34+
fmt.Printf("%s ", lang)
35+
}
2036
},
2137
}
2238

cli/runner/cmd/requests.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//file to contain helper functions for different commands
2+
3+
package cmd
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
)
11+
12+
// Simple struct to hold JSON vals from API call
13+
type Langs struct {
14+
Languages []string `json:"languages"`
15+
}
16+
17+
// This function is designed to send an API call to the specified endpoint.
18+
// The server parameter is designed to take in a URL, such as 'http://localhost:8080'.
19+
// The endpoint parameter is designed to take in the proper API endpoint, such as '/api/v1/languages'.
20+
// This function will return a pointer to the Langs struct which contains a list of the languages
21+
// currently supported by the API, and an error parameter in the case of failure.
22+
func getLangListJSON(server string, endpoint string) (*Langs, error) {
23+
resp, err := http.Get(server + endpoint)
24+
if err != nil {
25+
return nil, err
26+
} else if resp.StatusCode > 299 {
27+
return nil, fmt.Errorf("Response failed with status code: %d\n", resp.StatusCode)
28+
}
29+
defer resp.Body.Close()
30+
31+
body, err := io.ReadAll(resp.Body)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
var jsonLangs Langs
37+
json.Unmarshal(body, &jsonLangs)
38+
return &jsonLangs, fmt.Errorf("")
39+
}
40+
41+
/*
42+
func postSourceFile(server string, endpoint string, filepath string, explicit bool) string {
43+
return "placeholder"
44+
}
45+
*/

cli/runner/cmd/root.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import (
1414
var rootCmd = &cobra.Command{
1515
Use: "runner",
1616
Short: "A CLI to interact with the runner server",
17+
Long: `
18+
Example Usage:
19+
runner run source.py
20+
runner run -l source.py # Pre-check language before API call
21+
runner langs`,
1722
// Uncomment the following line if your bare application
1823
// has an action associated with it:
1924
// Run: func(cmd *cobra.Command, args []string) { },

cli/runner/cmd/run.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
// runCmd represents the run command
1414
var runCmd = &cobra.Command{
1515
Use: "run",
16-
Short: "A brief description of your command",
16+
Short: "runs the supplied code loaded in from a file.",
1717
Run: func(cmd *cobra.Command, args []string) {
18+
//TODO: check for explicit-lang flag and
1819
// implement CLI subcommand logic here
20+
1921
fmt.Println("run called")
2022
},
2123
}
@@ -32,4 +34,5 @@ func init() {
3234
// Cobra supports local flags which will only run when this command
3335
// is called directly, e.g.:
3436
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
37+
runCmd.Flags().BoolP("lang-check", "l", false, "check if the language is supported before the API call")
3538
}

0 commit comments

Comments
 (0)