11package main
22
33import (
4- "bufio"
54 "bytes"
5+ "errors"
66 "fmt"
77 "io"
88 "os"
@@ -44,7 +44,7 @@ func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error {
4444 return err
4545 }
4646 if _ , err = os .Stat (resolvedPath ); err == nil {
47- return fmt . Errorf (serviceAlreadyExistsWarn (resolvedPath ))
47+ return errors . New (serviceAlreadyExistsWarn (resolvedPath ))
4848 }
4949
5050 var buffer bytes.Buffer
@@ -118,49 +118,6 @@ func ensureConfigDirExists(configDir string) error {
118118 return err
119119}
120120
121- // openFile opens the file at path. If create is set and the file exists, returns nil, true, nil
122- func openFile (path string , create bool ) (file * os.File , exists bool , err error ) {
123- expandedPath , err := homedir .Expand (path )
124- if err != nil {
125- return nil , false , err
126- }
127- if create {
128- fileInfo , err := os .Stat (expandedPath )
129- if err == nil && fileInfo .Size () > 0 {
130- return nil , true , nil
131- }
132- file , err = os .OpenFile (expandedPath , os .O_RDWR | os .O_CREATE , 0600 )
133- } else {
134- file , err = os .Open (expandedPath )
135- }
136- return file , false , err
137- }
138-
139- func copyCredential (srcCredentialPath , destCredentialPath string ) error {
140- destFile , exists , err := openFile (destCredentialPath , true )
141- if err != nil {
142- return err
143- } else if exists {
144- // credentials already exist, do nothing
145- return nil
146- }
147- defer destFile .Close ()
148-
149- srcFile , _ , err := openFile (srcCredentialPath , false )
150- if err != nil {
151- return err
152- }
153- defer srcFile .Close ()
154-
155- // Copy certificate
156- _ , err = io .Copy (destFile , srcFile )
157- if err != nil {
158- return fmt .Errorf ("unable to copy %s to %s: %v" , srcCredentialPath , destCredentialPath , err )
159- }
160-
161- return nil
162- }
163-
164121func copyFile (src , dest string ) error {
165122 srcFile , err := os .Open (src )
166123 if err != nil {
@@ -187,36 +144,3 @@ func copyFile(src, dest string) error {
187144 ok = true
188145 return nil
189146}
190-
191- func copyConfig (srcConfigPath , destConfigPath string ) error {
192- // Copy or create config
193- destFile , exists , err := openFile (destConfigPath , true )
194- if err != nil {
195- return fmt .Errorf ("cannot open %s with error: %s" , destConfigPath , err )
196- } else if exists {
197- // config already exists, do nothing
198- return nil
199- }
200- defer destFile .Close ()
201-
202- srcFile , _ , err := openFile (srcConfigPath , false )
203- if err != nil {
204- fmt .Println ("Your service needs a config file that at least specifies the hostname option." )
205- fmt .Println ("Type in a hostname now, or leave it blank and create the config file later." )
206- fmt .Print ("Hostname: " )
207- reader := bufio .NewReader (os .Stdin )
208- input , _ := reader .ReadString ('\n' )
209- if input == "" {
210- return err
211- }
212- fmt .Fprintf (destFile , "hostname: %s\n " , input )
213- } else {
214- defer srcFile .Close ()
215- _ , err = io .Copy (destFile , srcFile )
216- if err != nil {
217- return fmt .Errorf ("unable to copy %s to %s: %v" , srcConfigPath , destConfigPath , err )
218- }
219- }
220-
221- return nil
222- }
0 commit comments