Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arishta-yb committed Oct 10, 2023
1 parent 3970d1d commit 28392f7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
62 changes: 44 additions & 18 deletions cmd/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ var getCmd = &cobra.Command{
authApi.GetInfo("", "")
startDate, _ := cmd.Flags().GetString("start")
endDate, _ := cmd.Flags().GetString("end")
clusters, _ := cmd.Flags().GetStringSlice("clusters")
outputFormat, _ := cmd.Flags().GetString("output-format")
outputFile, _ := cmd.Flags().GetString("output-file")
if outputFile == "" {
outputFile = "usage"
}

// Validate start date and end date
startDateTime, err := parseAndFormatDate(startDate)
if err != nil {
logrus.Fatalf("Invalid start date format. Use either RFC3339 format (e.g., '2023-09-01T12:30:45.000Z') or 'yyyy-MM-dd' format (e.g., '2023-09-01').\n")
Expand All @@ -68,12 +65,43 @@ var getCmd = &cobra.Command{
logrus.Fatalf("Invalid end date format. Use either RFC3339 format (e.g., '2023-09-01T12:30:45.000Z') or 'yyyy-MM-dd' format (e.g., '2023-09-01').\n")
}

if startDateTime > endDateTime {
if startDateTime.After(endDateTime) {
logrus.Fatalf("Start date must be before end date.")
}

startDate = startDateTime
endDate = endDateTime
startDate = startDateTime.Format("2006-01-02T15:04:05.000Z")
endDate = endDateTime.Format("2006-01-02T15:04:05.000Z")

// Assigning default value to filename if not specified
outputFileFormat := "usage_%s_%s"
if outputFile == "" {
startDateComponents := startDateTime.Format("20060102T150405") // Format as YYYYMMDDHHmmSS
endDateComponents := endDateTime.Format("20060102T150405") // Format as YYYYMMDDHHmmSS
outputFile = fmt.Sprintf(outputFileFormat, startDateComponents, endDateComponents)
}

// Check if the file already exists
if _, err := os.Stat(outputFile); err == nil {
logrus.Fatalf("File %s already exists. Please choose a different output file name.", outputFile)
}

// Fetching the cluster names from the input
var clusters []string
if cmd.Flags().Changed("cluster-name") {
clusterNames, _ := cmd.Flags().GetStringArray("cluster-name")
for _, clusterNameString := range clusterNames {
kvp := strings.Split(clusterNameString, "=")
if len(kvp) != 2 {
if len(kvp) != 2 {
logrus.Warnf("Incorrect format in cluster name: %s", clusterNameString)
}
continue
}

value := kvp[1]
clusters = append(clusters, value)
}
}

respC, r, err := authApi.ListClustersByDateRange(startDate, endDate).Execute()
if err != nil {
Expand Down Expand Up @@ -113,35 +141,33 @@ var getCmd = &cobra.Command{
switch strings.ToLower(outputFormat) {
case "csv":
if err := outputCSV(usageData, outputFile); err != nil {
return
logrus.Fatalf("Error outputting CSV: %v", err)
}
case "json":
if err := outputJSON(usageData, outputFile); err != nil {
return
logrus.Fatalf("Error outputting JSON: %v", err)
}
default:
logrus.Warnf("Unsupported format: %s. Defaulting to CSV.\n", outputFormat)
logrus.Warnf("Unsupported format: %s. Defaulting to CSV.", outputFormat)
if err := outputCSV(usageData, outputFile); err != nil {
return
logrus.Fatalf("Error outputting CSV: %v", err)
}
}
},
}

func parseAndFormatDate(dateStr string) (string, error) {
func parseAndFormatDate(dateStr string) (time.Time, error) {
// Try parsing in RFC3339 format
parsedDate, err := time.Parse(time.RFC3339, dateStr)
if err != nil {
// If parsing fails, try parsing in "yyyy-MM-dd" format
parsedDate, err = time.Parse("2006-01-02", dateStr)
if err != nil {
return "", err
return time.Time{}, err
}
}

// Always format in the desired "yyyy-MM-ddTHH:mm:ss.SSSZ" format
formattedDate := parsedDate.Format("2006-01-02T15:04:05.000Z")
return formattedDate, nil
return parsedDate, nil
}

func outputCSV(resp ybmclient.BillingUsageData, filename string) error {
Expand Down Expand Up @@ -219,7 +245,7 @@ func init() {
UsageCmd.AddCommand(getCmd)
getCmd.Flags().String("start", "", "[REQUIRED] Start date in RFC3339 format (e.g., '2023-09-01T12:30:45.000Z') or 'yyyy-MM-dd' format (e.g., '2023-09-01').")
getCmd.Flags().String("end", "", "[REQUIRED] End date in RFC3339 format (e.g., '2023-09-30T23:59:59.999Z') or 'yyyy-MM-dd' format (e.g., '2023-09-30').")
getCmd.Flags().StringSliceP("clusters", "c", []string{}, "[OPTIONAL] List of cluster names separated by comma.")
getCmd.Flags().StringArray("cluster-name", []string{}, `[OPTIONAL] Cluster names. Please provide key value pairs c1=<cluster1>,c2=<cluster2>.. as the value.`)
getCmd.Flags().String("output-format", "csv", "[OPTIONAL] Output format. Possible values: csv, json.")
getCmd.Flags().String("output-file", "usage", "[OPTIONAL] Output filename.")
getCmd.Flags().String("output-file", "", "[OPTIONAL] Output filename.")
}
12 changes: 6 additions & 6 deletions docs/ybm_usage_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ ybm usage get [flags]
### Options

```
-c, --clusters strings [OPTIONAL] List of cluster names separated by comma.
--end string [REQUIRED] End date in RFC3339 format (e.g., '2023-09-30T23:59:59.999Z') or 'yyyy-MM-dd' format (e.g., '2023-09-30').
-h, --help help for get
--output-file string [OPTIONAL] Output filename. (default "usage")
--output-format string [OPTIONAL] Output format. Possible values: csv, json. (default "csv")
--start string [REQUIRED] Start date in RFC3339 format (e.g., '2023-09-01T12:30:45.000Z') or 'yyyy-MM-dd' format (e.g., '2023-09-01').
--cluster-name stringArray [OPTIONAL] Cluster names. Please provide key value pairs c1=<cluster1>,c2=<cluster2>.. as the value.
--end string [REQUIRED] End date in RFC3339 format (e.g., '2023-09-30T23:59:59.999Z') or 'yyyy-MM-dd' format (e.g., '2023-09-30').
-h, --help help for get
--output-file string [OPTIONAL] Output filename.
--output-format string [OPTIONAL] Output format. Possible values: csv, json. (default "csv")
--start string [REQUIRED] Start date in RFC3339 format (e.g., '2023-09-01T12:30:45.000Z') or 'yyyy-MM-dd' format (e.g., '2023-09-01').
```

### Options inherited from parent commands
Expand Down

0 comments on commit 28392f7

Please sign in to comment.