-
Notifications
You must be signed in to change notification settings - Fork 7k
Support -f/--file flag in argocd app add
#35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
dc132a1
Add -f/--file flag to "app add" command
798e1c0
Add basic readLocalFile method
b276d77
Don't ignore return values
1554322
Don't read local file into string
77e3800
Unmarshal read file once it's read successfully
7fc4bf1
Update flag requirements to allow file flag
c68173c
Re-add initial usage test
89d8879
Add functions, tests for reading remote file
232b885
Add ability to load file remotely in app.go
c7f19bb
Don't need extra spaghetti logic when using log.Fatal
f7dfe33
Support both JSON and YAML
5a76c1e
Handle JSON and YAML properly now
b0e2cf7
Add URL validation function and test case
1cc0575
Fix typo, blame @merenbach
6f26e2e
Factor unmarshaling into util.go; no dir support
ffe9347
Fix casing on var name
44aa675
Use URL validation instead of scheme checking
0041a4a
Rm unused import
a6ba8ab
Rename server=>serve to be more idiomatic
f3e87e0
Add some comments to util_test.go
5b5bd7e
Run goimports on util.go, thanks @alexmt
4b9255b
Rm redundant os.Exit, thanks @alexmt
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,49 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io/ioutil" | ||
| "log" | ||
| "net/http" | ||
|
|
||
| argoappv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" | ||
| "github.com/ghodss/yaml" | ||
| ) | ||
|
|
||
| // unmarshalApplication tries to convert a YAML or JSON byte array into an Application struct. | ||
| func unmarshalApplication(data []byte, app *argoappv1.Application) { | ||
| // first, try unmarshaling as JSON | ||
| // Based on technique from Kubectl, which supports both YAML and JSON: | ||
| // https://mlafeldt.github.io/blog/teaching-go-programs-to-love-json-and-yaml/ | ||
| // http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang/ | ||
| // Short version: JSON unmarshaling won't zero out null fields; YAML unmarshaling will. | ||
| // This may have unintended effects or hard-to-catch issues when populating our application object. | ||
| data, err := yaml.YAMLToJSON(data) | ||
| if err != nil { | ||
| log.Fatal("Could not decode valid JSON or YAML Kubernetes manifest") | ||
| } | ||
| err = json.Unmarshal(data, &app) | ||
| if err != nil { | ||
| log.Fatalf("Could not unmarshal Kubernetes manifest: %s", string(data)) | ||
| } | ||
| } | ||
|
|
||
| // readLocalFile reads a file from disk and returns its contents as a byte array. | ||
| // The caller is responsible for checking error return values. | ||
| func readLocalFile(path string) (data []byte, err error) { | ||
| data, err = ioutil.ReadFile(path) | ||
| return | ||
| } | ||
|
|
||
| // readRemoteFile issues a GET request to retrieve the contents of the specified URL as a byte array. | ||
| // The caller is responsible for checking error return values. | ||
| func readRemoteFile(url string) (data []byte, err error) { | ||
| resp, err := http.Get(url) | ||
| if err == nil { | ||
| defer func() { | ||
| _ = resp.Body.Close() | ||
| }() | ||
| data, err = ioutil.ReadAll(resp.Body) | ||
| } | ||
| return | ||
| } | ||
This file contains hidden or 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,64 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestReadLocalFile(t *testing.T) { | ||
| sentinel := "Hello, world!" | ||
|
|
||
| file, err := ioutil.TempFile(os.TempDir(), "") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer func() { | ||
| _ = os.Remove(file.Name()) | ||
| }() | ||
|
|
||
| _, _ = file.WriteString(sentinel) | ||
| _ = file.Sync() | ||
|
|
||
| data, err := readLocalFile(file.Name()) | ||
| if string(data) != sentinel { | ||
| t.Errorf("Test data did not match (err = %v)! Expected \"%s\" and received \"%s\"", err, sentinel, string(data)) | ||
| } | ||
| } | ||
|
|
||
| func TestReadRemoteFile(t *testing.T) { | ||
| sentinel := "Hello, world!" | ||
|
|
||
| serve := func(c chan<- string) { | ||
| // listen on first available dynamic (unprivileged) port | ||
| listener, err := net.Listen("tcp", ":0") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // send back the address so that it can be used | ||
| c <- listener.Addr().String() | ||
|
|
||
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| // return the sentinel text at root URL | ||
| fmt.Fprint(w, sentinel) | ||
| }) | ||
|
|
||
| panic(http.Serve(listener, nil)) | ||
| } | ||
|
|
||
| c := make(chan string, 1) | ||
|
|
||
| // run a local webserver to test data retrieval | ||
| go serve(c) | ||
|
|
||
| address := <-c | ||
| data, err := readRemoteFile("http://" + address) | ||
| t.Logf("Listening at address: %s", address) | ||
| if string(data) != sentinel { | ||
| t.Errorf("Test data did not match (err = %v)! Expected \"%s\" and received \"%s\"", err, sentinel, string(data)) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking. Please run
goimports -w cmd/argocd/commands/util.go. We usually automatically run goimports on file save, so next person might get unexpected changes.