github.com/paketoio/libpak@v1.3.1/internal/formatter.go (about) 1 /* 2 * Copyright 2018-2020 the original author or 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 * https://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 internal 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 24 "github.com/buildpacks/libcnb" 25 "github.com/heroku/color" 26 ) 27 28 // LaunchFormatter is the formatter for a collection of environment variables. 29 type EnvironmentFormatter struct { 30 Path string 31 Environment map[string]string 32 } 33 34 func (e EnvironmentFormatter) String() string { 35 var s []string 36 37 for k, _ := range e.Environment { 38 s = append(s, fmt.Sprintf("Writing %s/%s", e.Path, k)) 39 } 40 41 sort.Strings(s) 42 return strings.Join(s, "\n") 43 } 44 45 // LaunchFormatter is the formatter for a libcnb.Launch. 46 type LaunchFormatter libcnb.Launch 47 48 func (l LaunchFormatter) String() string { 49 var s []string 50 51 if len(l.Slices) > 0 { 52 s = append(s, fmt.Sprintf("%s", SlicesFormatter(l.Slices))) 53 } 54 55 if len(l.Processes) > 0 { 56 s = append(s, fmt.Sprintf("Process types:\n%s", ProcessesFormatter(l.Processes))) 57 } 58 59 return strings.Join(s, "\n") 60 } 61 62 // ProcessesFormatter is the formatter for a []libcnb.Process. 63 type ProcessesFormatter []libcnb.Process 64 65 func (p ProcessesFormatter) String() string { 66 sort.Slice(p, func(i int, j int) bool { 67 return p[i].Type < p[j].Type 68 }) 69 70 var s []string 71 72 max := p.maximumTypeLength() 73 for _, p := range p { 74 sb := strings.Builder{} 75 sb.WriteString(fmt.Sprintf(" %s: ", color.CyanString(p.Type))) 76 77 for i := 0; i < max-len(p.Type); i++ { 78 sb.WriteString(" ") 79 } 80 81 sb.WriteString(p.Command) 82 83 for _, a := range p.Arguments { 84 sb.WriteString(fmt.Sprintf(" %s", a)) 85 } 86 87 if p.Direct { 88 sb.WriteString(" (direct)") 89 } 90 91 s = append(s, sb.String()) 92 } 93 94 return strings.Join(s, "\n") 95 } 96 97 func (p ProcessesFormatter) maximumTypeLength() int { 98 max := 0 99 100 for _, t := range p { 101 if l := len(t.Type); l > max { 102 max = l 103 } 104 } 105 106 return max 107 } 108 109 // SlicesFormatter is the formatter for a []libcnb.Slice. 110 type SlicesFormatter []libcnb.Slice 111 112 func (s SlicesFormatter) String() string { 113 return fmt.Sprintf("%d application slices", len(s)) 114 } 115 116 // StoreFormatter is the formatter for a libcnb.Store. 117 type StoreFormatter libcnb.Store 118 119 func (s StoreFormatter) String() string { 120 var n []string 121 122 for k, _ := range s.Metadata { 123 n = append(n, k) 124 } 125 126 sort.Strings(n) 127 return fmt.Sprintf("Writing persistent metadata: %s", strings.Join(n, ", ")) 128 }