Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Add Download command
Browse files Browse the repository at this point in the history
Download is similar to cat, excepts it download the configuration and
save it as file with detector name as file name on current folder
  • Loading branch information
VijayanB committed Sep 15, 2020
1 parent fd5f782 commit 9f6437f
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions cli/cmd/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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"

"github.com/spf13/cobra"
)

const commandDownload = "download"

//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(d *entity.DetectorOutput) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
filePath := filepath.Join(cwd, d.Name)
f, err := os.Create(filePath)
defer func() {
f.Close()
}()
if err != nil {
return err
}
return FPrint(f, d)
}

func init() {
esadCmd.AddCommand(downloadCmd)
downloadCmd.Flags().BoolP("name", "", true, "input is name or pattern")
downloadCmd.Flags().BoolP("id", "", false, "input is id")
}

0 comments on commit 9f6437f

Please sign in to comment.