experimental.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2024 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package experimental
  14. import (
  15. "context"
  16. "os"
  17. "strconv"
  18. "github.com/docker/compose/v2/internal/desktop"
  19. )
  20. // envComposeExperimentalGlobal can be set to a falsy value (e.g. 0, false) to
  21. // globally opt-out of any experimental features in Compose.
  22. const envComposeExperimentalGlobal = "COMPOSE_EXPERIMENTAL"
  23. // State of experiments (enabled/disabled) based on environment and local config.
  24. type State struct {
  25. // active is false if experiments have been opted-out of globally.
  26. active bool
  27. desktopValues desktop.FeatureFlagResponse
  28. }
  29. func NewState() *State {
  30. // experimental features have individual controls, but users can opt out
  31. // of ALL experiments easily if desired
  32. experimentsActive := true
  33. if v := os.Getenv(envComposeExperimentalGlobal); v != "" {
  34. experimentsActive, _ = strconv.ParseBool(v)
  35. }
  36. return &State{
  37. active: experimentsActive,
  38. }
  39. }
  40. func (s *State) Load(ctx context.Context, client *desktop.Client) error {
  41. if !s.active {
  42. // user opted out of experiments globally, no need to load state from
  43. // Desktop
  44. return nil
  45. }
  46. if client == nil {
  47. // not running under Docker Desktop
  48. return nil
  49. }
  50. desktopValues, err := client.FeatureFlags(ctx)
  51. if err != nil {
  52. return err
  53. }
  54. s.desktopValues = desktopValues
  55. return nil
  56. }
  57. func (s *State) NavBar() bool {
  58. return s.determineFeatureState("ComposeNav")
  59. }
  60. func (s *State) AutoFileShares() bool {
  61. return s.determineFeatureState("ComposeAutoFileShares")
  62. }
  63. func (s *State) determineFeatureState(name string) bool {
  64. if s == nil || !s.active || s.desktopValues == nil {
  65. return false
  66. }
  67. // TODO(milas): we should add individual environment variable overrides
  68. // per-experiment in a generic way here
  69. return s.desktopValues[name].Enabled
  70. }