Skip to content

Commit

Permalink
fix apache#1230: make some commands to work offline
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and astefanutti committed Jan 31, 2020
1 parent c2a3512 commit d07c6ed
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
41 changes: 41 additions & 0 deletions e2e/offline_commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// +build integration

// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration"

/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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
http://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 e2e

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestKamelVersionWorksOffline(t *testing.T) {
assert.Nil(t, kamel("version", "--config", "non-existent-kubeconfig-file").Execute())
}

func TestKamelHelpTraitWorksOffline(t *testing.T) {
assert.Nil(t, kamel("help", "trait", "--all", "--config", "non-existent-kubeconfig-file").Execute())
}

func TestKamelCompletionWorksOffline(t *testing.T) {
assert.Nil(t, kamel("completion", "bash", "--config", "non-existent-kubeconfig-file").Execute())
assert.Nil(t, kamel("completion", "zsh", "--config", "non-existent-kubeconfig-file").Execute())
}
3 changes: 3 additions & 0 deletions pkg/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func newCmdCompletionBash(root *cobra.Command) *cobra.Command {
fmt.Print(err.Error())
}
},
Annotations: map[string]string{
offlineCommandLabel: "true",
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/completion_zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func newCmdCompletionZsh(root *cobra.Command) *cobra.Command {
fmt.Print(err.Error())
}
},
Annotations: map[string]string{
offlineCommandLabel: "true",
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func addHelpSubCommands(cmd *cobra.Command, options *RootCmdOptions) error {
}

func (command *RootCmdOptions) preRun(cmd *cobra.Command, _ []string) error {
if command.Namespace == "" {
if command.Namespace == "" && !isOfflineCommand(cmd) {
var current string
client, err := command.GetCmdClient()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/trait_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func newTraitHelpCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *traitHelp
}
return options.run(args)
},
Annotations: map[string]string{
offlineCommandLabel: "true",
},
}

cmd.Flags().Bool("all", false, "Include all traits")
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
offlineCommandLabel = "camel.apache.org/cmd.offline"
)

// DeleteIntegration --
func DeleteIntegration(ctx context.Context, c client.Client, name string, namespace string) error {
integration := v1.Integration{
Expand Down Expand Up @@ -218,3 +222,7 @@ func stringToSliceHookFunc(comma rune) mapstructure.DecodeHookFunc {
func cmdOnly(cmd *cobra.Command, options interface{}) *cobra.Command {
return cmd
}

func isOfflineCommand(cmd *cobra.Command) bool {
return cmd.Annotations[offlineCommandLabel] == "true"
}
3 changes: 3 additions & 0 deletions pkg/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ func newCmdVersion() *cobra.Command {
Run: func(_ *cobra.Command, _ []string) {
fmt.Println("Camel K Client " + defaults.Version)
},
Annotations: map[string]string{
offlineCommandLabel: "true",
},
}
}

0 comments on commit d07c6ed

Please sign in to comment.