Go implementation of Brace Expansion mechanism to generate arbitrary strings.

Overview

Bash Brace Expansion in Go

Go implementation of Brace Expansion mechanism to generate arbitrary strings.

Implementing: 3.5.1 Brace Expansion

PkgGoDev license GitHub last commit Mentioned in Awesome Go

tests Go Report Card Coverage Status

benchmarks

Usage

go get github.com/mkungla/bexp/v3

Get string slice

package main

import (
  "fmt"

  "github.com/mkungla/bexp/v3"
)

func main() {
  var v []string
  v = bexp.Parse("file-{a,b,c}.jpg")
  fmt.Println(v)
  // [file-a.jpg file-b.jpg file-c.jpg]

  v = bexp.Parse("-v{,,}")
  fmt.Println(v)
  // [-v -v -v]

  v = bexp.Parse("file{0..2}.jpg")
  fmt.Println(v)
  // [file0.jpg file1.jpg file2.jpg]

  v = bexp.Parse("file{2..0}.jpg")
  fmt.Println(v)
  // [file2.jpg file1.jpg file0.jpg]

  v = bexp.Parse("file{0..4..2}.jpg")
  fmt.Println(v)
  // [file0.jpg file2.jpg file4.jpg]

  v = bexp.Parse("file-{a..e..2}.jpg")
  fmt.Println(v)
  // [file-a.jpg file-c.jpg file-e.jpg]

  v = bexp.Parse("file{00..10..5}.jpg")
  fmt.Println(v)
  // [file00.jpg file05.jpg file10.jpg]

  v = bexp.Parse("{{A..C},{a..c}}")
  fmt.Println(v)
  // [A B C a b c]

  v = bexp.Parse("ppp{,config,oe{,conf}}")
  fmt.Println(v)
  // [ppp pppconfig pppoe pppoeconf]

  v = bexp.Parse("data/{P1/{10..19},P2/{20..29},P3/{30..39}}")
  fmt.Println(v)
  // [data/P1/10 data/P1/11 data/P1/12 data/P1/13 data/P1/14 data/P1/15 data/P1/16 data/P1/17 data/P1/18 data/P1/19 data/P2/20 data/P2/21 data/P2/22 data/P2/23 data/P2/24 data/P2/25 data/P2/26 data/P2/27 data/P2/28 data/P2/29 data/P3/30 data/P3/31 data/P3/32 data/P3/33 data/P3/34 data/P3/35 data/P3/36 data/P3/37 data/P3/38 data/P3/39]
}

Generating directory tree

package main

import (
  "log"

  "github.com/mkungla/bexp/v3"
)

func main() {
  const (
    rootdir = "/tmp/bexp"
    treeexp = rootdir + "/{dir1,dir2,dir3/{subdir1,subdir2}}"
  )
  if err := bexp.MkdirAll(treeexp, 0750); err != nil {
    log.Fatal(err)
  }

  // Will produce directory tree
  // /tmp/bexp
  // /tmp/bexp/dir1
  // /tmp/bexp/dir2
  // /tmp/bexp/dir3
  // /tmp/bexp/dir3/subdir1
  // /tmp/bexp/dir3/subdir2
}

Expand URLS

The example shows how to create Openstreetmap tiles
around the desired latitude and longitude coordinates.

package main

import (
  "fmt"
  "log"
  "math"

  "github.com/mkungla/bexp/v3"
)

func getCenterTile(lat, long float64, zoom int) (z, x, y int) {
  n := math.Exp2(float64(zoom))
  x = int(math.Floor((long + 180.0) / 360.0 * n))
  if float64(x) >= n {
    x = int(n - 1)
  }
  y = int(math.Floor((1.0 - math.Log(
    math.Tan(lat*math.Pi/180.0)+
      1.0/math.Cos(lat*math.Pi/180.0))/
    math.Pi) / 2.0 * n))
  return x, y, zoom
}

func main() {
  x, y, z := getCenterTile(51.03, 13.78, 5)
  pattern := fmt.Sprintf(
    "https://tile.openstreetmap.org/%d/{%d..%d}/{%d..%d}.png",
    z, x-2, x+2, y-2, y+2,
  )
  tiles := bexp.Parse(pattern)
  fmt.Println("pattern: ", pattern)
  for _, tile := range tiles {
    fmt.Println(tile)
  }
  // Output:
  // pattern: https://tile.openstreetmap.org/5/{15..19}/{8..12}.png
  // https://tile.openstreetmap.org/5/15/8.png
  // https://tile.openstreetmap.org/5/15/9.png
  // https://tile.openstreetmap.org/5/15/10.png
  // https://tile.openstreetmap.org/5/15/11.png
  // https://tile.openstreetmap.org/5/15/12.png
  // https://tile.openstreetmap.org/5/16/8.png
  // https://tile.openstreetmap.org/5/16/9.png
  // https://tile.openstreetmap.org/5/16/10.png
  // https://tile.openstreetmap.org/5/16/11.png
  // https://tile.openstreetmap.org/5/16/12.png
  // https://tile.openstreetmap.org/5/17/8.png
  // https://tile.openstreetmap.org/5/17/9.png
  // https://tile.openstreetmap.org/5/17/10.png
  // https://tile.openstreetmap.org/5/17/11.png
  // https://tile.openstreetmap.org/5/17/12.png
  // https://tile.openstreetmap.org/5/18/8.png
  // https://tile.openstreetmap.org/5/18/9.png
  // https://tile.openstreetmap.org/5/18/10.png
  // https://tile.openstreetmap.org/5/18/11.png
  // https://tile.openstreetmap.org/5/18/12.png
  // https://tile.openstreetmap.org/5/19/8.png
  // https://tile.openstreetmap.org/5/19/9.png
  // https://tile.openstreetmap.org/5/19/10.png
  // https://tile.openstreetmap.org/5/19/11.png
  // https://tile.openstreetmap.org/5/19/12.png
}

Need error checking?

package main

import (
  "errors"
  "fmt"

  "github.com/mkungla/bexp/v3"
)

func main() {
  empty, err := bexp.ParseValid("")
  fmt.Printf("%q - %t\n", empty[0], errors.Is(err, bexp.ErrEmptyResult))

  abc, err := bexp.ParseValid("abc")
  fmt.Printf("%q - %t\n", abc[0], errors.Is(err, bexp.ErrUnchangedBraceExpansion))

  // Output:
  // "" - true
  // "abc" - true
}

With os.Expand

os.Expand

package main

import (
  "fmt"
  "os"

  "github.com/mkungla/bexp/v3"
)

func main() {
  const treeExp = "$MY_ROOT_DIR/dir{1..3}/{subdir1,subdir2}"
  mapper := func(varName string) string {
    switch varName {
    case "MY_ROOT_DIR":
      return "/my_root"
    }
    return ""
  }
  str := os.Expand(treeExp, mapper)
  fmt.Println("str := os.Expand(treeExp, mapper)")
  fmt.Println(str)

  fmt.Println("v := bexp.Parse(str)")
  v := bexp.Parse(str)
  for _, p := range v {
    fmt.Println(p)
  }

  // Output:
  // str := os.Expand(treeExp, mapper)
  // /my_root/dir{1..3}/{subdir1,subdir2}
  // v := bexp.Parse(str)
  // /my_root/dir1/subdir1
  // /my_root/dir1/subdir2
  // /my_root/dir2/subdir1
  // /my_root/dir2/subdir2
  // /my_root/dir3/subdir1
  // /my_root/dir3/subdir2
}

With os.ExpandEnv

os.ExpandEnv

package main

import (
  "fmt"
  "os"

  "github.com/mkungla/bexp/v3"
)

func main() {
  const treeExp = "$MY_ROOT_DIR/dir{1..3}/{subdir1,subdir2}"
  os.Setenv("MY_ROOT_DIR", "/my_root")
  
  str := os.ExpandEnv(treeExp)
  fmt.Println("str := os.ExpandEnv(treeExp)")
  fmt.Println(str)
  
  fmt.Println("v := bexp.Parse(str)")
  v := bexp.Parse(str)
  for _, p := range v {
    fmt.Println(p)
  }
  
  // Output:
  // str := os.ExpandEnv(treeExp)
  // /my_root/dir{1..3}/{subdir1,subdir2}
  // v := bexp.Parse(str)
  // /my_root/dir1/subdir1
  // /my_root/dir1/subdir2
  // /my_root/dir2/subdir1
  // /my_root/dir2/subdir2
  // /my_root/dir3/subdir1
  // /my_root/dir3/subdir2
}

Inspired by and other similar libraries

following package were inspiration to create this package, The motivation of this package is to improve performance and reduce memory allocations compared to other solutions. Also to add some commonly used API's when working with brace expansion strings

Releases(v3.0.0)
Owner
Marko Kungla
Software Architect/Engineer. WebXR, DevOps and Golang enthusiast.
Marko Kungla
Small, fast library to create ANSI colored strings and codes. [go, golang]

ansi Package ansi is a small, fast library to create ANSI colored strings and codes. Install Get it go get -u github.com/mgutz/ansi Example import "gi

Mario Gutierrez 364 Jul 10, 2022
Library for easy named formatting strings

go-celsium Library for easy named formatting translations Documentation All translations with named parameters are stored in next format: Hello, {name

Freon Service 4 Apr 12, 2021
linenoise-classic is a command-line tool that generates strings of random characters that can be used as reasonably secure passwords.

linenoise-classic is a command-line tool that generates strings of random characters that can be used as reasonably secure passwords.

Mark Cornick 0 Dec 31, 2021
Go cmd utility that prints its command line arguments using strings.Join

Results This is an exercise of the book The Go Programming Language, by Alan A. A. Donovan and Brian Kernighan. Comparison between different versions

Santiago Rodriguez 0 Dec 18, 2021
Zenstring - Randomly generated, friendly & zen strings for your app

zenstring Randomly generated, friendly & zen strings for your app. Started as a

Ciprian 1 Feb 3, 2022
Generate flags by parsing structures

Flags based on structures. The sflags package uses structs, reflection and struct field tags to allow you specify command line options. It supports di

null 140 Aug 1, 2022
Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Aditya Mukerjee 2.5k Aug 8, 2022
Generate High Level Cloud Architecture diagrams using YAML syntax.

A commandline tool that generate High Level microservice & serverless Architecture diagrams using a declarative syntax defined in a YAML file.

Luca Sepe 542 Aug 11, 2022
A CLI Tool to easily generate your Terraform configuration

Tf Tf is a command line tool to easily generate your Terraform configuration with an interactive prompt. Inspiration Boredom in Covid-19 Installation

Gowtham Munukutla 19 Jul 26, 2022
Generate Changelogs from Commits (CLI)

Commitlog Changelog generator using Commit History To see an example of this in action, you can check the actions file for this repo. Yes it uses itse

Reaper 70 Aug 8, 2022
CLI tool for CIDR range operations (check, generate)

cidrchk A CLI tool to assist you with CIDR ranges and IPs. Install it You can download the latest binary for Linux (Intel and Arm), macOS, and Windows

Michael Hausenblas 129 Aug 4, 2022
Generate QR-Codes for checking into events using the official Corona Warn App.

Corona Warn App QR-Code Generator Generate QR-Codes for checking into events using the official Corona Warn App. Table of Contents Introduction Instal

Lukas Malkmus 7 Nov 6, 2021
An alternative syntax to generate YAML (or JSON) from commandline

yo An alternative syntax to generate YAML (or JSON) from commandline. The ultimate commanline YAML (or JSON) generator! ... I'm kidding of course! but

Luca Sepe 10 Jul 30, 2022
Commandline tool to generate Cistercian numerals

cistercian Commandline tool to generate Cistercian numerals. Installation go get github.com/rhardih/cistercian Example usage Text $ cistercian 7323

René Hansen 41 Jul 30, 2022
Generate ANSI-/Ascii-art version images/Gifs in your terminal.

ANSI-Art NOTE: This toy project is not yet finished. ANSI-version Logo Block ANSI-version Logo ASCII-version Logo Support Platform You are kindly remi

Weiran Huang 42 Jul 10, 2022
Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. 🔍 📊

askgit is a command-line tool for running SQL queries on git repositories. It's meant for ad-hoc querying of git repositories on disk through a common interface (SQL), as an alternative to patching together various shell commands.

AskGit 3.1k Aug 15, 2022
A tool to manage all your boilerplate from cli and generate files for you !

A tool to manage all your boilerplate from cli and generate files for you !

null 28 Jul 20, 2022
✍ A package and command line tool to generate Lorem Ipsum

Lorelai A package and command line tool to generate Lorem ipsum. Install go get github.com/UltiRequiem/lorelai/pkg Examples Generating Text func print

Eliaz Bobadilla 24 Jul 27, 2022
A simple CLI applet to generate 'White Noise' pictorial depictions.

White Noise Generator A simple CLI applet written in Go to create pictorial representations of White Noise. Example Okay, but how? An output file of t

Michal Špano 1 Jun 18, 2022