feat: add COMPOSE_FILE env variable

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
This commit is contained in:
AhmedGrati 2023-11-05 15:02:36 +01:00
parent 032b61a656
commit a91b57ef2e
5 changed files with 124 additions and 1 deletions

View File

@ -21,6 +21,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Logrus hooks
@ -80,6 +82,16 @@ var RootCmd = &cobra.Command{
if provider != "kubernetes" && provider != "openshift" {
log.Fatalf("%s is an unsupported provider. Supported providers are: 'kubernetes', 'openshift'.", GlobalProvider)
}
v := viper.New()
v.BindEnv("file", "COMPOSE_FILE")
cmd.Flags().VisitAll(func(f *pflag.Flag) {
configName := f.Name
if configName == "file" && !f.Changed && v.IsSet(configName) {
GlobalFiles = v.GetStringSlice(configName)
}
})
},
}
@ -93,6 +105,6 @@ func init() {
RootCmd.PersistentFlags().BoolVarP(&GlobalVerbose, "verbose", "v", false, "verbose output")
RootCmd.PersistentFlags().BoolVar(&GlobalSuppressWarnings, "suppress-warnings", false, "Suppress all warnings")
RootCmd.PersistentFlags().BoolVar(&GlobalErrorOnWarning, "error-on-warning", false, "Treat any warning as an error")
RootCmd.PersistentFlags().StringArrayVarP(&GlobalFiles, "file", "f", []string{}, "Specify an alternative compose file")
RootCmd.PersistentFlags().StringSliceVarP(&GlobalFiles, "file", "f", []string{}, "Specify an alternative compose file")
RootCmd.PersistentFlags().StringVar(&GlobalProvider, "provider", "kubernetes", "Specify a provider. Kubernetes or OpenShift.")
}

View File

@ -310,3 +310,7 @@ convert::expect_success "$os_cmd" "$os_output" || exit 1
cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/no-profile-warning/docker-compose.yaml convert"
convert::expect_warning "$cmd" "No service selected. The profile specified in services of your compose yaml may not exist." || exit 1
# Test COMPOSE_FILE env variable is honored
k8s_cmd="COMPOSE_FILE=\"$KOMPOSE_ROOT/script/test/fixtures/compose-file-env-variable/docker-compose.yaml $KOMPOSE_ROOT/script/test/fixtures/compose-file-env-variable/alternative-docker-compose.yaml\" kompose convert --stdout --with-kompose-annotation=false"
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/fixtures/compose-file-env-variable/output-k8s.yaml"
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1

View File

@ -0,0 +1,7 @@
version: "3"
services:
alpine:
image: alpine
ports:
- 80:80

View File

@ -0,0 +1,6 @@
version: "3"
services:
debian:
image: debian
ports:
- 80:80

View File

@ -0,0 +1,94 @@
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: alpine
name: alpine
spec:
ports:
- name: "80"
port: 80
targetPort: 80
selector:
io.kompose.service: alpine
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: debian
name: debian
spec:
ports:
- name: "80"
port: 80
targetPort: 80
selector:
io.kompose.service: debian
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: alpine
name: alpine
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: alpine
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/compose-file-env-variable-default: "true"
io.kompose.service: alpine
spec:
containers:
- image: alpine
name: alpine
ports:
- containerPort: 80
hostPort: 80
protocol: TCP
resources: {}
restartPolicy: Always
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: debian
name: debian
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: debian
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.network/compose-file-env-variable-default: "true"
io.kompose.service: debian
spec:
containers:
- image: debian
name: debian
ports:
- containerPort: 80
hostPort: 80
protocol: TCP
resources: {}
restartPolicy: Always