|
| 1 | +/* |
| 2 | + Copyright 2024 Docker Compose CLI authors |
| 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 | + |
| 17 | +package experimental |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "os" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + "github.com/docker/compose/v2/internal/desktop" |
| 25 | + "github.com/sirupsen/logrus" |
| 26 | +) |
| 27 | + |
| 28 | +// envComposeExperimentalGlobal can be set to a falsy value (e.g. 0, false) to |
| 29 | +// globally opt-out of any experimental features in Compose. |
| 30 | +const envComposeExperimentalGlobal = "COMPOSE_EXPERIMENTAL" |
| 31 | + |
| 32 | +// FeatureFlagClient queries feature flag state from local configuration. |
| 33 | +type FeatureFlagClient interface { |
| 34 | + FeatureFlags(ctx context.Context) (desktop.FeatureFlagResponse, error) |
| 35 | +} |
| 36 | + |
| 37 | +// State of experiments (enabled/disabled) based on environment and local config. |
| 38 | +type State struct { |
| 39 | + // enabled is false if experiments have been opted-out of globally. |
| 40 | + enabled bool |
| 41 | + desktopValues desktop.FeatureFlagResponse |
| 42 | +} |
| 43 | + |
| 44 | +func NewState(ctx context.Context, client FeatureFlagClient) State { |
| 45 | + // by default, experiments are enabled, but can be opted-out via an env var |
| 46 | + enabled := true |
| 47 | + if v := os.Getenv(envComposeExperimentalGlobal); v != "" { |
| 48 | + enabled, _ = strconv.ParseBool(v) |
| 49 | + } |
| 50 | + |
| 51 | + var desktopValues desktop.FeatureFlagResponse |
| 52 | + if enabled && client != nil { |
| 53 | + var err error |
| 54 | + desktopValues, err = client.FeatureFlags(ctx) |
| 55 | + if err != nil { |
| 56 | + logrus.Debugf("Failed to query feature flags from Desktop: %v", err) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return State{ |
| 61 | + enabled: enabled, |
| 62 | + desktopValues: desktopValues, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (m *State) NavBar() bool { |
| 67 | + return m.determineFeatureState("ComposeNavBar") |
| 68 | +} |
| 69 | + |
| 70 | +func (m *State) AutoFileShares() bool { |
| 71 | + return m.determineFeatureState("ComposeAutoFileShares") |
| 72 | +} |
| 73 | + |
| 74 | +func (m *State) determineFeatureState(name string) bool { |
| 75 | + if m.enabled || m.desktopValues == nil { |
| 76 | + return false |
| 77 | + } |
| 78 | + return m.desktopValues[name].Enabled |
| 79 | +} |
0 commit comments