-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from napptive/PG-134/Remove_app_command
Add application commands
- Loading branch information
Showing
24 changed files
with
481 additions
and
49 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Copyright 2021 Napptive | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License 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 commands | ||
|
||
import ( | ||
"github.com/napptive/catalog-manager/internal/app/cli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var adminCmdLongHelp = `admin commands` | ||
var adminCmdShortHelp = `admin commands` | ||
|
||
var adminCmd = &cobra.Command{ | ||
Use: "admin", | ||
Long: adminCmdLongHelp, | ||
Short: adminCmdShortHelp, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
|
||
var listCmdLongHelp = `list the catalog applications` | ||
var listCmdShortHelp = `list the catalog applications` | ||
var listCmd = &cobra.Command{ | ||
Use: "list [namespace]", | ||
Long: listCmdLongHelp, | ||
Short: listCmdShortHelp, | ||
Aliases: []string{"ls"}, | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
namespace := "" | ||
if len(args) > 0 { | ||
namespace = args[0] | ||
} | ||
op, err := cli.NewApplicationCli(cfg.AdminGRPCPort) | ||
if err != nil { | ||
return err | ||
} | ||
return op.List(namespace) | ||
}, | ||
} | ||
|
||
var deleteAppCmdLongHelp = `Delete applications from catalog. | ||
You can delete a namespace indicating the name as arg or delete only one application by passing the name as namespace/applicationName` | ||
var deleteAppCmdShortHelp = `Delete catalog application` | ||
|
||
var deleteAppCmd = &cobra.Command{ | ||
Use: "delete <applicationName>", | ||
Long: deleteAppCmdLongHelp, | ||
Short: deleteAppCmdShortHelp, | ||
Aliases: []string{"rm", "remove"}, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
op, err := cli.NewApplicationCli(cfg.AdminGRPCPort) | ||
if err != nil { | ||
return err | ||
} | ||
return op.Delete(args[0]) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(adminCmd) | ||
|
||
adminCmd.AddCommand(deleteAppCmd) | ||
adminCmd.AddCommand(listCmd) | ||
|
||
adminCmd.PersistentFlags().IntVar(&cfg.CatalogManager.AdminGRPCPort, "adminGRPCPort", 7062, "gRPC Port to connect the Catalog-manager admin API") | ||
} |
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
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,83 @@ | ||
/** | ||
* Copyright 2021 Napptive | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License 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 cli | ||
|
||
import ( | ||
"fmt" | ||
grpc_catalog_go "github.com/napptive/grpc-catalog-go" | ||
"github.com/napptive/nerrors/pkg/nerrors" | ||
"github.com/rs/zerolog/log" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type ApplicationCli struct { | ||
// adminClient to connect to Admin interface | ||
adminClient grpc_catalog_go.NamespaceAdministrationClient | ||
} | ||
|
||
func NewApplicationCli(adminPort int) (*ApplicationCli, error) { | ||
dir := fmt.Sprintf(":%d", adminPort) | ||
log.Info().Str("dir", dir).Msg("admin direction") | ||
conn, err := grpc.Dial(dir, grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := grpc_catalog_go.NewNamespaceAdministrationClient(conn) | ||
return &ApplicationCli{ | ||
adminClient: client, | ||
}, nil | ||
} | ||
|
||
// Delete removes applications from the catalog | ||
// if app is an applicationName removes it and | ||
// if app is a namespace removes all the namespace applications | ||
func (ac *ApplicationCli) Delete(app string) error { | ||
log.Debug().Str("appName", app).Msg("Delete") | ||
|
||
if app == "" { | ||
return nerrors.NewFailedPreconditionError("applicationName or namespace mut be filled") | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
// check if app is an applicationName or is a namespace | ||
// applicationName -> namespace/appName | ||
// namespace -> namespace | ||
if strings.Contains(app, "/") { | ||
response, err := ac.adminClient.DeleteApplication(ctx, &grpc_catalog_go.RemoveApplicationRequest{ApplicationId: app}) | ||
PrintResultOrError(response, err) | ||
} else { | ||
// is a namespace | ||
response, err := ac.adminClient.Delete(ctx, &grpc_catalog_go.DeleteNamespaceRequest{Namespace: app}) | ||
PrintResultOrError(response, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ac *ApplicationCli) List(namespace string) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
response, err := ac.adminClient.List(ctx, &grpc_catalog_go.ListApplicationsRequest{Namespace: namespace}) | ||
PrintResultOrError(response, err) | ||
|
||
return nil | ||
} |
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,43 @@ | ||
/** | ||
* Copyright 2021 Napptive | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License 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 cli | ||
|
||
import ( | ||
"fmt" | ||
"github.com/napptive/catalog-manager/internal/pkg/printer" | ||
|
||
"github.com/napptive/nerrors/pkg/nerrors" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
// PrintResultOrError prints the result using a given printer or the error. | ||
func PrintResultOrError(result interface{}, err error) { | ||
if err != nil { | ||
if zerolog.GlobalLevel() == zerolog.DebugLevel { | ||
fmt.Println(nerrors.FromError(err).StackTraceToString()) | ||
} else { | ||
fmt.Println(err.Error()) | ||
} | ||
} else { | ||
if pErr := printer.NewTablePrinter().Print(result); pErr != nil { | ||
if zerolog.GlobalLevel() == zerolog.DebugLevel { | ||
fmt.Println(nerrors.FromError(pErr).StackTraceToString()) | ||
} else { | ||
fmt.Println(pErr.Error()) | ||
} | ||
} | ||
} | ||
} |
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
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,23 @@ | ||
/** | ||
* Copyright 2021 Napptive | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License 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 printer | ||
|
||
// ResultPrinter defines the operations that a printer must define. Multiple printer are | ||
// offered depending on the desired output format. | ||
type ResultPrinter interface { | ||
// Print the result. | ||
Print(result interface{}) error | ||
} |
Oops, something went wrong.