app_build.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package flags
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. )
  6. const (
  7. // App_BuildMethod is the key for the build method flag
  8. App_BuildMethod = "build-method"
  9. // App_Dockerfile is the key for the dockerfile flag
  10. App_Dockerfile = "dockerfile"
  11. // App_Builder is the key for the builder flag
  12. App_Builder = "builder"
  13. // App_Buildpacks is the key for the buildpacks flag
  14. App_Buildpacks = "attach-buildpacks"
  15. // App_BuildContext is the key for the build context flag
  16. App_BuildContext = "build-context"
  17. // App_NoBuild is the key for the no build flag
  18. App_NoBuild = "no-build"
  19. // App_NoPull is the key for the no pull flag
  20. App_NoPull = "no-pull"
  21. )
  22. // UseAppBuildFlags adds build flags to the given command
  23. func UseAppBuildFlags(cmd *cobra.Command) {
  24. cmd.PersistentFlags().String(
  25. App_BuildMethod,
  26. "",
  27. "set the build method for the app on apply, either 'docker' or 'pack'",
  28. )
  29. cmd.PersistentFlags().String(
  30. App_Dockerfile,
  31. "",
  32. "set the path to the Dockerfile when build method is 'docker'",
  33. )
  34. cmd.PersistentFlags().String(
  35. App_Builder,
  36. "",
  37. "set the builder to use when build method is 'pack'",
  38. )
  39. cmd.PersistentFlags().StringSlice(
  40. App_Buildpacks,
  41. nil,
  42. "attach buildpacks to use when build method is 'pack'",
  43. )
  44. cmd.PersistentFlags().String(
  45. App_BuildContext,
  46. "",
  47. "set the build context for the app",
  48. )
  49. }
  50. type buildValues struct {
  51. BuildMethod string
  52. Dockerfile string
  53. Builder string
  54. Buildpacks []string
  55. BuildContext string
  56. }
  57. // AppBuildValuesFromCmd retrieves build values from command flags
  58. func AppBuildValuesFromCmd(cmd *cobra.Command) (buildValues, error) {
  59. var values buildValues
  60. buildMethod, err := cmd.Flags().GetString(App_BuildMethod)
  61. if err != nil {
  62. return values, fmt.Errorf("error getting build method: %s", err)
  63. }
  64. dockerfile, err := cmd.Flags().GetString(App_Dockerfile)
  65. if err != nil {
  66. return values, fmt.Errorf("error getting dockerfile: %s", err)
  67. }
  68. builder, err := cmd.Flags().GetString(App_Builder)
  69. if err != nil {
  70. return values, fmt.Errorf("error getting builder: %s", err)
  71. }
  72. buildpacks, err := cmd.Flags().GetStringSlice(App_Buildpacks)
  73. if err != nil {
  74. return values, fmt.Errorf("error getting buildpacks: %s", err)
  75. }
  76. buildContext, err := cmd.Flags().GetString(App_BuildContext)
  77. if err != nil {
  78. return values, fmt.Errorf("error getting build context: %s", err)
  79. }
  80. values = buildValues{
  81. BuildMethod: buildMethod,
  82. Dockerfile: dockerfile,
  83. Builder: builder,
  84. Buildpacks: buildpacks,
  85. BuildContext: buildContext,
  86. }
  87. return values, nil
  88. }