This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
CLI: Download Detector Configuration as File #229
Merged
VijayanB
merged 6 commits into
opendistro-for-elasticsearch:master
from
VijayanB:download
Sep 17, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f39278
Refactor cat command
VijayanB fd5f782
CLI: Refactor command to display error
VijayanB 9f6437f
Add Download command
VijayanB c29f835
CLI: Add interactive to prompt for overwriting files
VijayanB 41ad5d6
CLI: Allow user to specify output folder path
VijayanB 363e8c7
CLI: Make download prompt as default
VijayanB 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 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 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 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 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,107 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
entity "esad/internal/entity/ad" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
commandDownload = "download" | ||
flagOutput = "output" | ||
fileExtensionJSON = "json" | ||
) | ||
|
||
//downloadCmd downloads detectors configuration on current working directory | ||
//based on detector id or name patter | ||
var downloadCmd = &cobra.Command{ | ||
Use: commandDownload + " [flags] [list of detectors]", | ||
Short: "Downloads detectors configurations based on id or name pattern", | ||
Long: fmt.Sprintf("Description:\n " + | ||
`Downloads detectors configurations based on id or name pattern, use "" to make sure the name is not matched with pwd lists'`), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
//If no args, display usage | ||
if len(args) < 1 { | ||
displayError(cmd.Usage(), commandDownload) | ||
return | ||
} | ||
err := printDetectors(WriteInFile, cmd, args) | ||
displayError(err, commandDownload) | ||
}, | ||
} | ||
|
||
//WriteInFile writes detector's configuration on file | ||
//file will be created inside current working directory, | ||
//with detector name as file name | ||
func WriteInFile(cmd *cobra.Command, d *entity.DetectorOutput) error { | ||
output, _ := cmd.Flags().GetString(flagOutput) | ||
if _, err := os.Stat(output); os.IsNotExist(err) { | ||
return fmt.Errorf("output directory [%s] does not exists", output) | ||
} | ||
filePath := filepath.Join(output, fmt.Sprintf("%s.%s", d.Name, fileExtensionJSON)) | ||
if ok := isCreateFileAllowed(filePath); !ok { | ||
return nil | ||
} | ||
f, err := os.Create(filePath) | ||
defer func() { | ||
f.Close() | ||
}() | ||
if err != nil { | ||
return err | ||
} | ||
return FPrint(f, d) | ||
} | ||
|
||
func isCreateFileAllowed(path string) bool { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return true | ||
} | ||
return askForConfirmation(path) | ||
} | ||
|
||
func askForConfirmation(path string) bool { | ||
|
||
fmt.Printf("overwrite %s? (y/n [n])", filepath.Base(path)) | ||
var response string | ||
_, err := fmt.Fscanln(os.Stdin, &response) | ||
if err != nil { | ||
//Exit if for some reason, we are not able to accept user input | ||
fmt.Printf("failed to accept value from user due to %s", err) | ||
return false | ||
} | ||
switch strings.ToLower(response) { | ||
case "y", "yes": | ||
return true | ||
case "n", "no": | ||
return false | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func init() { | ||
esadCmd.AddCommand(downloadCmd) | ||
downloadCmd.Flags().BoolP("name", "", true, "input is name or pattern") | ||
downloadCmd.Flags().BoolP("id", "", false, "input is id") | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
fmt.Println("failed to find current working directory due to ", err) | ||
} | ||
downloadCmd.Flags().StringP(flagOutput, "o", cwd, "downloads detectors inside this folder path") | ||
} |
This file contains 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 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
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.
If the
filePath
exists, willos.Create
override the existing file?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.
From this doc, https://golang.org/pkg/os/#Create
How about check file exists or not before create ? If file exists, we should not truncate it directly in case user execute command by mistake.
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.
I introduced a flag -i similar to cp in linux commands, which prompt to stdout whether to overwrite the file or not.
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.
The flag
-i
is optional? What if user don't add-i
?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.
It is interactive. By default download will overwrite but if user passes -i, it will prompt for user confirmation. Do you think prompt should be default?
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.
I think prompt should be default for this case, not good user experience/safe to override user's existing file without any prompt.