contexts.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2020 Docker, Inc.
  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/api/config"
  17. "github.com/docker/api/context/store"
  18. contextsv1 "github.com/docker/api/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.ContextStore(ctx)
  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. result := &contextsv1.ListResponse{}
  40. for _, c := range contexts {
  41. result.Contexts = append(result.Contexts, &contextsv1.Context{
  42. Name: c.Name,
  43. ContextType: c.Type(),
  44. Current: c.Name == configFile.CurrentContext,
  45. })
  46. }
  47. return result, nil
  48. }