Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Aug 15, 2022
0 parents commit 00be95f
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
52 changes: 52 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
project_name: "cinful"

builds:
- binary: "cinful"
main: cmd/cinful/main.go

ldflags:
- -s -w
env:
- CGO_ENABLED=0

goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64

snapshot:
name_template: "{{ .Tag }}-SNAPSHOT-{{.ShortCommit}}"

archives:
- format: binary
replacements:
darwin: Darwin
linux: Linux
windows: Windows
amd64: x86_64
# Needed hack for binary only uploads
# For more information, check #602
files:
- "thisfiledoesnotexist*"


checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt'

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'


release:
disable: false
draft: false
github:
owner: hofstadter-io
name: cinful
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2022 Hofstadter, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Hofstadter Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# cinful

Golang library and CLI to detect if a program
is running in a CI system.

Pronounced "sinful" by mashing together CI, info, and the 'ful' suffix.

Originally ported from [watson/ci-info](https://github.com/watson/ci-info)
and [other links](https://adamj.eu/tech/2020/03/09/detect-if-your-tests-are-running-on-ci/)


### as a CLI

prints details if in CI, otherwise nothing

```sh
cinful

cinful list # show all
```

Binaries are available on the releases page.


### as a pkg

```go
package main

import (
"fmt"

"github.com/hofstadter-io/cinful"
)

func main() {
vendor := cinful.Info()
if vendor != nil {
fmt.Println(vendor)
}
}
```

where

```go
type Vendor struct {
Name string `json:"name,omitempty"`
Constant string `json:"constant,omitempty"`
Env any `json:"env,omitempty"`
PR any `json:"pr,omitempty"`
Val string `json:"val,omitempty"`
}
```

Env will be a string after checking,
but due to vendor differences, is a
string, list, or map prior.

### releasing

git must be clean first

release: `goreleaser --rm-dist -p 1`

snapshot (test): `goreleaser --rm-dist -p 1 --snapshot`
97 changes: 97 additions & 0 deletions cinful.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cinful

import (
_ "embed"
"encoding/json"
"fmt"
"os"
)

//go:embed vendors.json
var b []byte

func init() {
err := json.Unmarshal(b, &vendors)
if err != nil {
panic(err)
}
}

var vendors []Vendor

type Vendor struct {
Name string `json:"name,omitempty"`
Constant string `json:"constant,omitempty"`
Env any `json:"env,omitempty"`
PR any `json:"pr,omitempty"`
Val string `json:"val,omitempty"`
}

func (v *Vendor) String() string {
vfmt := "Name: %s\nConst: %s\nEnv: %v\nVal: %v"
return fmt.Sprintf(vfmt, v.Name, v.Constant, v.Env, v.Val)
}

func PrintVendors() {
for _, v := range vendors {
fmt.Printf("%#v\n", v)
}
}

func Info() *Vendor {
// check vendor first, for more details
for _, v := range vendors {
switch vt := v.Env.(type) {
// normally just a string
case string:
val := os.Getenv(vt)
if val != "" {
v.Val = val
return &v
}
// a list of strings
case []interface {}:
for _, ev := range vt {
val := os.Getenv(ev.(string))
if val != "" {
v.Env = ev.(string)
v.Val = val
return &v
}
}

// an ENV var with a specific value
case map[string]interface {}:
for ek, ev := range vt {
val := os.Getenv(ek)
if val == ev.(string) {
v.Env = ek
v.Val, _ = ev.(string)
return &v
}
}

}
}

// check some common ENV vars
for _, ce := range commonEnv {
val := os.Getenv(ce)
if val != "" {
return &Vendor{
Name: "Common Var",
Constant: "COMMON",
Env: ce,
Val: val,
}
}
}
return nil
}

var commonEnv = []string{
"CI",
"CONTINUOUS_INTEGRATION",
"BUILD_NUMBER",
"RUN_ID",
}
21 changes: 21 additions & 0 deletions cmd/cinful/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"os"

"github.com/hofstadter-io/cinful"
)

func main() {
if len(os.Args) == 2 && os.Args[1] == "list" {
cinful.PrintVendors()
return
}

vendor := cinful.Info()
if vendor != nil {
fmt.Println(vendor)
}
}

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hofstadter-io/cinful

go 1.18
Loading

0 comments on commit 00be95f

Please sign in to comment.