Skip to content

Commit 7649607

Browse files
committed
add asset commands
1 parent cfcd7f3 commit 7649607

File tree

4 files changed

+207
-1
lines changed

4 files changed

+207
-1
lines changed

cmd/asset.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © LiquidWeb
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"os"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var assetCmd = &cobra.Command{
25+
Use: "asset",
26+
Short: "All things assets.",
27+
Long: `An asset is an individual product on an account.
28+
29+
For a full list of capabilities, please refer to the "Available Commands" section.`,
30+
Run: func(cmd *cobra.Command, args []string) {
31+
if err := cmd.Help(); err != nil {
32+
lwCliInst.Die(err)
33+
}
34+
os.Exit(1)
35+
},
36+
}
37+
38+
func init() {
39+
rootCmd.AddCommand(assetCmd)
40+
}

cmd/assetDetails.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright © LiquidWeb
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
23+
"github.com/liquidweb/liquidweb-cli/types/api"
24+
)
25+
26+
var assetDetailsCmd = &cobra.Command{
27+
Use: "details",
28+
Short: "Get details of a specific asset",
29+
Long: `Get details of a specific asset.
30+
31+
An asset is an individual product on an account. Assets have categories.
32+
`,
33+
Run: func(cmd *cobra.Command, args []string) {
34+
jsonFlag, _ := cmd.Flags().GetBool("json")
35+
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
36+
37+
var details apiTypes.Subaccnt
38+
apiArgs := map[string]interface{}{
39+
"uniq_id": uniqIdFlag,
40+
"alsowith": []string{"categories"},
41+
}
42+
43+
if err := lwCliInst.CallLwApiInto("bleed/asset/details", apiArgs, &details); err != nil {
44+
lwCliInst.Die(err)
45+
}
46+
47+
if jsonFlag {
48+
pretty, err := lwCliInst.JsonEncodeAndPrettyPrint(details)
49+
if err != nil {
50+
lwCliInst.Die(err)
51+
}
52+
fmt.Printf(pretty)
53+
} else {
54+
fmt.Print(details)
55+
}
56+
},
57+
}
58+
59+
func init() {
60+
assetCmd.AddCommand(assetDetailsCmd)
61+
62+
assetDetailsCmd.Flags().Bool("json", false, "output in json format")
63+
assetDetailsCmd.Flags().String("uniq-id", "", "uniq-id of the asset")
64+
65+
if err := assetDetailsCmd.MarkFlagRequired("uniq-id"); err != nil {
66+
lwCliInst.Die(err)
67+
}
68+
}

cmd/assetList.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright © LiquidWeb
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
23+
"github.com/liquidweb/liquidweb-cli/instance"
24+
"github.com/liquidweb/liquidweb-cli/types/api"
25+
)
26+
27+
var assetListCmdCategoriesFlag []string
28+
29+
var assetListCmd = &cobra.Command{
30+
Use: "list",
31+
Short: "List assets on your account",
32+
Long: `List assets on your account.
33+
34+
An asset is an individual product on an account. Assets have categories.
35+
`,
36+
Run: func(cmd *cobra.Command, args []string) {
37+
jsonFlag, _ := cmd.Flags().GetBool("json")
38+
39+
apiArgs := map[string]interface{}{
40+
"alsowith": []string{"categories"},
41+
}
42+
43+
if len(assetListCmdCategoriesFlag) > 0 {
44+
apiArgs["category"] = assetListCmdCategoriesFlag
45+
}
46+
47+
methodArgs := instance.AllPaginatedResultsArgs{
48+
Method: "bleed/asset/list",
49+
ResultsPerPage: 100,
50+
MethodArgs: apiArgs,
51+
}
52+
results, err := lwCliInst.AllPaginatedResults(&methodArgs)
53+
if err != nil {
54+
lwCliInst.Die(err)
55+
}
56+
57+
if jsonFlag {
58+
pretty, err := lwCliInst.JsonEncodeAndPrettyPrint(results)
59+
if err != nil {
60+
lwCliInst.Die(err)
61+
}
62+
fmt.Printf(pretty)
63+
} else {
64+
cnt := 1
65+
for _, item := range results.Items {
66+
67+
var details apiTypes.Subaccnt
68+
if err := instance.CastFieldTypes(item, &details); err != nil {
69+
lwCliInst.Die(err)
70+
}
71+
72+
fmt.Printf("%d.) ", cnt)
73+
fmt.Print(details)
74+
cnt++
75+
}
76+
}
77+
},
78+
}
79+
80+
func init() {
81+
assetCmd.AddCommand(assetListCmd)
82+
83+
assetListCmd.Flags().Bool("json", false, "output in json format")
84+
85+
assetListCmd.Flags().StringSliceVar(&assetListCmdCategoriesFlag, "categories",
86+
[]string{}, "categories to include separated by ','")
87+
88+
}

types/api/subaccnt.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ func (x Subaccnt) String() string {
2424

2525
slice = append(slice, fmt.Sprintf("Domain: %s UniqId: %s\n", x.Domain, x.UniqId))
2626

27-
slice = append(slice, fmt.Sprintf("\tIp: %s\n", x.Ip))
27+
if len(x.Categories) > 0 {
28+
slice = append(slice, fmt.Sprintln("\tCategories"))
29+
for _, category := range x.Categories {
30+
slice = append(slice, fmt.Sprintf("\t\t* %s\n", category))
31+
}
32+
}
33+
34+
if x.Ip != "" && x.Ip != "127.0.0.1" {
35+
slice = append(slice, fmt.Sprintf("\tIp: %s\n", x.Ip))
36+
}
37+
2838
if x.ProjectName != "" && x.ProjectId != 0 {
2939
slice = append(slice, fmt.Sprintf("\tProjectName: %s (id %d)\n", x.ProjectName, x.ProjectId))
3040
}

0 commit comments

Comments
 (0)