github.com/cs3org/reva/v2@v2.27.7/tools/create-artifacts/main.go (about)

     1  // Copyright 2018-2022 CERN
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package main
    20  
    21  import (
    22  	"bytes"
    23  	"crypto/sha256"
    24  	"encoding/hex"
    25  	"flag"
    26  	"fmt"
    27  	"io"
    28  	"log"
    29  	"os"
    30  	"os/exec"
    31  	"time"
    32  )
    33  
    34  var (
    35  	dev       = flag.Bool("dev", false, "if dev is set to true creates dev builds with commit and build date")
    36  	commit    = flag.String("commit", "", "sets git commit")
    37  	version   = flag.String("version", "", "sets git version")
    38  	goVersion = flag.String("goversion", "", "sets go version")
    39  	buildDate = time.Now().Format("2006-01-02")
    40  
    41  	binaries = []string{"reva", "revad"}
    42  	archs    = []string{"386", "amd64"}
    43  	oses     = []string{"linux", "darwin"}
    44  )
    45  
    46  func init() {
    47  	flag.Parse()
    48  
    49  	if (*commit == "" || *goVersion == "") && (*version == "" && !*dev) {
    50  		fmt.Fprint(os.Stderr, "fill all the flags\n")
    51  		os.Exit(1)
    52  	}
    53  
    54  	// if version is not set we use the dev build setting build date and commit number.
    55  	if *version == "" {
    56  		*version = fmt.Sprintf("%s_%s", time.Now().Format("2006_01_02T150405"), *commit)
    57  	}
    58  }
    59  
    60  func main() {
    61  	if err := os.RemoveAll("dist"); err != nil {
    62  		fmt.Fprintf(os.Stderr, "error removing dist folder: %s", err)
    63  		os.Exit(1)
    64  	}
    65  
    66  	if err := os.MkdirAll("dist", 0755); err != nil {
    67  		fmt.Fprintf(os.Stderr, "error creating dist folder: %s", err)
    68  		os.Exit(1)
    69  	}
    70  
    71  	ldFlags := fmt.Sprintf("-s -X main.buildDate=%s -X main.gitCommit=%s -X main.version=%s -X main.goVersion=%s",
    72  		buildDate,
    73  		*commit,
    74  		*version,
    75  		*goVersion,
    76  	)
    77  
    78  	for _, bin := range binaries {
    79  		for _, o := range oses {
    80  			for _, arch := range archs {
    81  				if o == "darwin" && arch == "386" { // https://golang.org/doc/go1.14#darwin
    82  					continue
    83  				}
    84  				out := fmt.Sprintf("./dist/%s_%s_%s_%s", bin, *version, o, arch)
    85  				args := []string{"build", "-o", out, "-ldflags", ldFlags, "./cmd/" + bin}
    86  				cmd := exec.Command("go", args...)
    87  				cmd.Env = os.Environ()
    88  				cmd.Env = append(cmd.Env, []string{"GOOS=" + o, "GOARCH=" + arch}...)
    89  				cmd.Dir = "." // root of the repo
    90  				run(cmd)
    91  				hashFile(out)
    92  			}
    93  		}
    94  	}
    95  }
    96  
    97  func run(cmd *exec.Cmd) {
    98  	var b bytes.Buffer
    99  	mw := io.MultiWriter(os.Stdout, &b)
   100  	cmd.Stdout = mw
   101  	cmd.Stderr = mw
   102  	err := cmd.Run()
   103  	fmt.Println(cmd.Dir, cmd.Args)
   104  	fmt.Println(b.String())
   105  	if err != nil {
   106  		fmt.Println("ERROR: ", err.Error())
   107  		os.Exit(1)
   108  	}
   109  }
   110  
   111  func hashFile(file string) {
   112  	hasher := sha256.New()
   113  	f, err := os.Open(file)
   114  	if err != nil {
   115  		log.Fatal(err)
   116  	}
   117  	if _, err := io.Copy(hasher, f); err != nil {
   118  		f.Close()
   119  		log.Fatal(err)
   120  	}
   121  	f.Close()
   122  	val := hex.EncodeToString(hasher.Sum(nil))
   123  	if err := os.WriteFile(file+".sha256", []byte(val), 0644); err != nil {
   124  		log.Fatal(err)
   125  	}
   126  }