Skip to content

Commit

Permalink
Add support for reading password file
Browse files Browse the repository at this point in the history
  • Loading branch information
rxbn committed Mar 31, 2022
1 parent 101a0c3 commit 4812a00
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": [
"config:base"
],
"labels": ["dependencies"],
"postUpdateOptions": ["gomodTidy"]
}
9 changes: 6 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
paths-ignore:
- docs/**
- .github/**
pull_request:
paths-ignore:
- docs/**
- .github/**

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Determine Go version from go.mod
run: echo "GO_VERSION=$(grep "go 1." go.mod | cut -d " " -f 2)" >> $GITHUB_ENV
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --issues-exit-code=0 --timeout=3m ./...
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ A small CLI tool to generate a Docker `config.json` with registry credentials. I

## Usage

**Note:** `username`, `password` and `server` are required!
**Note:** `username`, `password` or `password-file` and `server` are required!

```
-b, --base64 output result base64 encoded
-p, --password string password for docker registry
-s, --server string docker registry server
-u, --username string username for docker registry
-v, --version print version and exit
-b, --base64 output result base64 encoded
-p, --password string password for docker registry
-f, --password-file string password file for docker registry
-s, --server string docker registry server
-u, --username string username for docker registry
-v, --version Print the current version and exit
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/containeroo/dologen

go 1.16
go 1.18

require github.com/spf13/pflag v1.0.5
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package main

import (
"bytes"
"encoding/base64"
"fmt"
flag "github.com/spf13/pflag"
"io/ioutil"
"os"
)

const version = "1.0.1"
const version = "1.1.0"

func main() {
username := flag.StringP("username", "u", "", "username for docker registry")
password := flag.StringP("password", "p", "", "password for docker registry")
passwordFile := flag.StringP("password-file", "f", "", "password file for docker registry")
server := flag.StringP("server", "s", "", "docker registry server")
base64Output := flag.BoolP("base64", "b", false, "output result base64 encoded")
printVersion := flag.BoolP("version", "v", false, "Print the current version and exit")
Expand All @@ -27,7 +30,7 @@ func main() {
flag.Usage()
os.Exit(1)
}
if *password == "" {
if *password == "" && *passwordFile == "" {
fmt.Println("password cannot be empty!")
flag.Usage()
os.Exit(1)
Expand All @@ -38,13 +41,25 @@ func main() {
os.Exit(1)
}

if *passwordFile != "" {
passwordBytes, err := ioutil.ReadFile(*passwordFile)
if err != nil {
fmt.Println("read password file error:", err)
os.Exit(1)
}

passwordBytes = bytes.Replace(passwordBytes, []byte("\n"), []byte(""), -1)
*password = string(passwordBytes)
}

auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", *username, *password)))

result := fmt.Sprintf("{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"auth\":\"%s\"}}}", *server, *username, *password, auth)

if *base64Output {
switch *base64Output {
case true:
fmt.Println(base64.StdEncoding.EncodeToString([]byte(result)))
} else {
default:
fmt.Println(result)
}
}
5 changes: 0 additions & 5 deletions renovate.json

This file was deleted.

0 comments on commit 4812a00

Please sign in to comment.