github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/build/buildfile/buildfile.go (about) 1 package buildfile 2 3 import ( 4 "bytes" 5 "fmt" 6 ) 7 8 type Buildfile struct { 9 bytes.Buffer 10 } 11 12 func New() *Buildfile { 13 b := Buildfile{} 14 b.WriteString(base) 15 return &b 16 } 17 18 // WriteCmd writes a command to the build file. The 19 // command will be echoed back as a base16 encoded 20 // command so that it can be parsed and appended to 21 // the build output 22 func (b *Buildfile) WriteCmd(command string) { 23 // echo the command as an encoded value 24 b.WriteString(fmt.Sprintf("echo '#DRONE:%x'\n", command)) 25 // and then run the command 26 b.WriteString(fmt.Sprintf("%s\n", command)) 27 } 28 29 // WriteCmdSilent writes a command to the build file 30 // but does not echo the command. 31 func (b *Buildfile) WriteCmdSilent(command string) { 32 b.WriteString(fmt.Sprintf("%s\n", command)) 33 } 34 35 // WriteComment adds a comment to the build file. This 36 // is really only used internally for debugging purposes. 37 func (b *Buildfile) WriteComment(comment string) { 38 b.WriteString(fmt.Sprintf("#%s\n", comment)) 39 } 40 41 // WriteEnv exports the environment variable as 42 // part of the script. The environment variables 43 // are not echoed back to the console, and are 44 // kept private by default. 45 func (b *Buildfile) WriteEnv(key, value string) { 46 b.WriteString(fmt.Sprintf("export %s=%s\n", key, value)) 47 } 48 49 // WriteHost adds an entry to the /etc/hosts file. 50 func (b *Buildfile) WriteHost(mapping string) { 51 b.WriteCmdSilent(fmt.Sprintf("[ -f /usr/bin/sudo ] || echo %q | tee -a /etc/hosts", mapping)) 52 b.WriteCmdSilent(fmt.Sprintf("[ -f /usr/bin/sudo ] && echo %q | sudo tee -a /etc/hosts", mapping)) 53 } 54 55 // every build script starts with the following 56 // code at the start. 57 var base = ` 58 #!/bin/bash 59 60 # drone configuration files are stored in /etc/drone.d 61 # execute these files prior to our build to set global 62 # environment variables and initialize programs (like rbenv) 63 if [ -d /etc/drone.d ]; then 64 for i in /etc/drone.d/*.sh; do 65 if [ -r $i ]; then 66 . $i 67 fi 68 done 69 unset i 70 fi 71 72 # be sure to exit on error and print out 73 # our bash commands, so we can which commands 74 # are executing and troubleshoot failures. 75 set -e 76 77 # user-defined commands below ############################## 78 `