contexts.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2020 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 proxy
  14. import (
  15. "context"
  16. "github.com/docker/compose-cli/api/config"
  17. "github.com/docker/compose-cli/api/context/store"
  18. contextsv1 "github.com/docker/compose-cli/cli/server/protos/contexts/v1"
  19. )
  20. type contextsProxy struct {
  21. configDir string
  22. }
  23. func (cp *contextsProxy) SetCurrent(ctx context.Context, request *contextsv1.SetCurrentRequest) (*contextsv1.SetCurrentResponse, error) {
  24. if err := config.WriteCurrentContext(cp.configDir, request.GetName()); err != nil {
  25. return &contextsv1.SetCurrentResponse{}, err
  26. }
  27. return &contextsv1.SetCurrentResponse{}, nil
  28. }
  29. func (cp *contextsProxy) List(ctx context.Context, request *contextsv1.ListRequest) (*contextsv1.ListResponse, error) {
  30. s := store.Instance()
  31. configFile, err := config.LoadFile(cp.configDir)
  32. if err != nil {
  33. return nil, err
  34. }
  35. contexts, err := s.List()
  36. if err != nil {
  37. return &contextsv1.ListResponse{}, err
  38. }
  39. return convertContexts(contexts, configFile.CurrentContext), nil
  40. }
  41. func convertContexts(contexts []*store.DockerContext, currentContext string) *contextsv1.ListResponse {
  42. result := &contextsv1.ListResponse{}
  43. for _, c := range contexts {
  44. endpointName := c.Type()
  45. if c.Type() == store.DefaultContextType {
  46. endpointName = "docker"
  47. }
  48. var endpoint interface{} = c.Endpoints[endpointName]
  49. context := contextsv1.Context{
  50. Name: c.Name,
  51. ContextType: c.Type(),
  52. Description: c.Metadata.Description,
  53. Current: c.Name == currentContext,
  54. }
  55. switch c.Type() {
  56. case store.DefaultContextType:
  57. context.Endpoint = getDockerEndpoint(endpoint)
  58. case store.AciContextType:
  59. context.Endpoint = getAciEndpoint(endpoint)
  60. case store.EcsContextType:
  61. context.Endpoint = getEcsEndpoint(endpoint)
  62. }
  63. result.Contexts = append(result.Contexts, &context)
  64. }
  65. return result
  66. }
  67. func getDockerEndpoint(endpoint interface{}) *contextsv1.Context_DockerEndpoint {
  68. typedEndpoint := endpoint.(*store.Endpoint)
  69. return &contextsv1.Context_DockerEndpoint{
  70. DockerEndpoint: &contextsv1.DockerEndpoint{
  71. Host: typedEndpoint.Host,
  72. },
  73. }
  74. }
  75. func getAciEndpoint(endpoint interface{}) *contextsv1.Context_AciEndpoint {
  76. typedEndpoint := endpoint.(*store.AciContext)
  77. return &contextsv1.Context_AciEndpoint{
  78. AciEndpoint: &contextsv1.AciEndpoint{
  79. ResourceGroup: typedEndpoint.ResourceGroup,
  80. Region: typedEndpoint.Location,
  81. SubscriptionId: typedEndpoint.SubscriptionID,
  82. },
  83. }
  84. }
  85. func getEcsEndpoint(endpoint interface{}) *contextsv1.Context_EcsEndpoint {
  86. typedEndpoint := endpoint.(*store.EcsContext)
  87. return &contextsv1.Context_EcsEndpoint{
  88. EcsEndpoint: &contextsv1.EcsEndpoint{
  89. FromEnvironment: typedEndpoint.CredentialsFromEnv,
  90. Profile: typedEndpoint.Profile,
  91. },
  92. }
  93. }