Clear out (#2)
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
@@ -1,44 +0,0 @@
|
|||||||
name: Go
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ main ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ main ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v5
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v6
|
|
||||||
with:
|
|
||||||
go-version: '1.26.2'
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: |
|
|
||||||
echo "Initializing config"
|
|
||||||
mkdir -p ~/.ssh
|
|
||||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender_models_deploy_key
|
|
||||||
chmod 600 ~/.ssh/textsender_models_deploy_key
|
|
||||||
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
|
||||||
|
|
||||||
eval $(ssh-agent -s)
|
|
||||||
ssh-add -v ~/.ssh/textsender_models_deploy_key
|
|
||||||
|
|
||||||
go env -w GOPRIVATE='${{ secrets.GIT_HOST_ROOT }}'
|
|
||||||
|
|
||||||
echo "Creating local .gitconfig"
|
|
||||||
touch ~/.gitconfig
|
|
||||||
cat > ~/.gitconfig << "EOF"
|
|
||||||
[url "ssh://git@${{ secrets.GIT_HOST_ROOT }}"]
|
|
||||||
insteadOf = https://${{ secrets.GIT_HOST_ROOT }}
|
|
||||||
EOF
|
|
||||||
|
|
||||||
make build
|
|
||||||
|
|
||||||
- name: Test
|
|
||||||
run: go test -v ./...
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
/vendor
|
|
||||||
/clean_file
|
|
||||||
numbers.json
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
VERSION ?= $(shell git describe --tags 2>/dev/null || echo "dev")
|
|
||||||
COMMIT ?= $(shell git rev-parse --short HEAD)
|
|
||||||
BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
|
|
||||||
GO_VERSION ?= $(shell go version | awk '{print $$3}')
|
|
||||||
|
|
||||||
.PHONY: build
|
|
||||||
build:
|
|
||||||
go build -ldflags="\
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.Version=$(VERSION)' \
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.BuildTime=$(BUILD_TIME)' \
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.Commit=$(COMMIT)' \
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.GoVersion=$(GO_VERSION)'" \
|
|
||||||
-o clean_file cmd/clean_file/main.go
|
|
||||||
|
|
||||||
.PHONY: install
|
|
||||||
install:
|
|
||||||
go install -ldflags="\
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.Version=$(VERSION)' \
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.BuildTime=$(BUILD_TIME)' \
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.Commit=$(COMMIT)' \
|
|
||||||
-X 'git.kundeng.us/phoenix/clean_file/internal/version.GoVersion=$(GO_VERSION)'"
|
|
||||||
-o clean_file cmd/clean_file/main.go
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
CLI software that processes a text file containing US phone numbers
|
|
||||||
and sanitizes the numbers. The result is contained in a json file.
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
```
|
|
||||||
make build
|
|
||||||
```
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/clean_file/internal/parser"
|
|
||||||
"git.kundeng.us/phoenix/clean_file/internal/version"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args
|
|
||||||
|
|
||||||
if len(args) < 2 {
|
|
||||||
log.Println("Invalid arguments provided")
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
versionFlag := flag.Bool("version", false, "Print version information")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if *versionFlag {
|
|
||||||
fmt.Println(version.String())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("clean_file")
|
|
||||||
filepath := args[1]
|
|
||||||
|
|
||||||
log.Println("File path:", filepath)
|
|
||||||
prsr := parser.NumberParser{FilePath: filepath}
|
|
||||||
|
|
||||||
numbers := prsr.FileDump()
|
|
||||||
prsr.PrintValues(numbers, true)
|
|
||||||
|
|
||||||
prsr.SaveFile(numbers)
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
module git.kundeng.us/phoenix/clean_file
|
|
||||||
|
|
||||||
go 1.26.2
|
|
||||||
|
|
||||||
require git.kundeng.us/phoenix/textsender-models v0.2.1
|
|
||||||
|
|
||||||
require github.com/google/uuid v1.6.0 // indirect
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
git.kundeng.us/phoenix/textsender-models v0.2.1 h1:21br4NF58aUFuCx8laKxC5RvZMl4GsSIaMX4bvf5plw=
|
|
||||||
git.kundeng.us/phoenix/textsender-models v0.2.1/go.mod h1:nu5QWy9o+spx/t9NFipaGmF5qiBJS/0QhxyCjoi3Z3E=
|
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
|
||||||
@@ -1,169 +0,0 @@
|
|||||||
package parser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
|
|
||||||
)
|
|
||||||
|
|
||||||
const Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
|
|
||||||
type NumberParser struct {
|
|
||||||
FilePath string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr *NumberParser) FileDump() []contact.Contact {
|
|
||||||
log.Println("Dumping files")
|
|
||||||
objs := []contact.Contact{}
|
|
||||||
|
|
||||||
for _, line := range prsr.readLines(prsr.FilePath) {
|
|
||||||
if strings.ContainsAny(line, Letters) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
myObj := contact.Contact{PhoneNumber: line}
|
|
||||||
if myObj.PhoneNumber != "" {
|
|
||||||
objs = append(objs, myObj)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
objs = prsr.updateContacts(objs)
|
|
||||||
objs = prsr.removeDups(objs)
|
|
||||||
|
|
||||||
return objs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr *NumberParser) updateContacts(contacts []contact.Contact) []contact.Contact {
|
|
||||||
updated := []contact.Contact{}
|
|
||||||
|
|
||||||
for _, ct := range contacts {
|
|
||||||
parsed := ct.PhoneNumber
|
|
||||||
if len(parsed) < 2 {
|
|
||||||
log.Println("Invalid number:", parsed)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
pars := prsr.removeSomeData(parsed)
|
|
||||||
parsed = pars
|
|
||||||
|
|
||||||
log.Println("Parsed:", parsed)
|
|
||||||
|
|
||||||
updatedParsed := prsr.addPrefix(parsed)
|
|
||||||
updated = append(updated, contact.Contact{PhoneNumber: updatedParsed})
|
|
||||||
}
|
|
||||||
|
|
||||||
return updated
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr *NumberParser) removeDups(contacts []contact.Contact) []contact.Contact {
|
|
||||||
updated := []contact.Contact{}
|
|
||||||
uniqueItems := make(map[string]string)
|
|
||||||
|
|
||||||
for _, val := range contacts {
|
|
||||||
uniqueItems[val.PhoneNumber] = val.PhoneNumber
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range uniqueItems {
|
|
||||||
updated = append(updated, contact.Contact{PhoneNumber: item})
|
|
||||||
}
|
|
||||||
|
|
||||||
return updated
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) PrintValues(contacts []contact.Contact, printTotal bool) {
|
|
||||||
fmt.Println("Printing numbers")
|
|
||||||
|
|
||||||
for _, ct := range contacts {
|
|
||||||
fmt.Println("Number:", ct.PhoneNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
if printTotal {
|
|
||||||
fmt.Println("Total Numbers:", len(contacts))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) SaveFile(vals []contact.Contact) {
|
|
||||||
filename := "numbers.json"
|
|
||||||
|
|
||||||
// Create json and save it to the filesystem
|
|
||||||
jsonData, err := json.MarshalIndent(&vals, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error marshaling JSON:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(filename, jsonData, 0644)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error writing file:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) readLines(filename string) []string {
|
|
||||||
// Open the file
|
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// Create a scanner to read the file line by line
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
lines := []string{}
|
|
||||||
|
|
||||||
// Read line by line
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
lines = append(lines, line)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for any errors during scanning
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) removeSomeData(unparsed string) string {
|
|
||||||
parsed := ""
|
|
||||||
|
|
||||||
for _, ch := range unparsed {
|
|
||||||
if ch == ' ' {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
if unicode.IsDigit(ch) {
|
|
||||||
parsed += string(ch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Parsed:", parsed)
|
|
||||||
|
|
||||||
return parsed
|
|
||||||
}
|
|
||||||
|
|
||||||
func (prsr NumberParser) addPrefix(raw string) string {
|
|
||||||
parsed := raw
|
|
||||||
|
|
||||||
firstChar := raw[0]
|
|
||||||
secondChar := raw[1]
|
|
||||||
|
|
||||||
if firstChar == '1' {
|
|
||||||
parsed = "+" + parsed
|
|
||||||
} else if firstChar != '+' {
|
|
||||||
if secondChar != '1' {
|
|
||||||
parsed = "+1" + parsed
|
|
||||||
} else {
|
|
||||||
parsed = "+1" + parsed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsed
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package parser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
|
|
||||||
)
|
|
||||||
|
|
||||||
const Test_File_Path = "../../tests/numbers.txt"
|
|
||||||
|
|
||||||
// Remove duplicates
|
|
||||||
func Test_RemoveDups(t *testing.T) {
|
|
||||||
parser := NumberParser{FilePath: Test_File_Path}
|
|
||||||
finalAmount := parser.FileDump()
|
|
||||||
|
|
||||||
if len(finalAmount) < 1 {
|
|
||||||
t.Error("Dump should not be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
testValues := []contact.Contact{
|
|
||||||
{PhoneNumber: "+12175550194"},
|
|
||||||
{PhoneNumber: "+16025550177"},
|
|
||||||
{PhoneNumber: "+17135550167"},
|
|
||||||
{PhoneNumber: "+16085550190"},
|
|
||||||
{PhoneNumber: "+13055550112"},
|
|
||||||
{PhoneNumber: "+14155550135"},
|
|
||||||
{PhoneNumber: "+13175550123"},
|
|
||||||
{PhoneNumber: "+13605550158"},
|
|
||||||
}
|
|
||||||
|
|
||||||
aSize := len(finalAmount)
|
|
||||||
bSize := len(testValues)
|
|
||||||
if aSize != bSize {
|
|
||||||
t.Error("Data has varrying sizes A", aSize, " B", bSize)
|
|
||||||
}
|
|
||||||
if !dataEqual(finalAmount, testValues) {
|
|
||||||
fmt.Println("Printing final amount")
|
|
||||||
for _, val := range finalAmount {
|
|
||||||
fmt.Println("PhoneNumber:", val.PhoneNumber)
|
|
||||||
}
|
|
||||||
fmt.Println("\nPrinting test values")
|
|
||||||
for _, val := range testValues {
|
|
||||||
fmt.Println("PhoneNumber:", val.PhoneNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Error("Data has not been updated")
|
|
||||||
} else {
|
|
||||||
log.Println("The duplicates have been removed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func dataEqual(a, b []contact.Contact) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range a {
|
|
||||||
foundIt := false
|
|
||||||
for j := range b {
|
|
||||||
if a[i] == b[j] {
|
|
||||||
foundIt = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !foundIt {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package version
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
var (
|
|
||||||
Version = "dev"
|
|
||||||
BuildTime = "unknown"
|
|
||||||
Commit = "unknown"
|
|
||||||
GoVersion = "unknown"
|
|
||||||
)
|
|
||||||
|
|
||||||
func String() string {
|
|
||||||
return fmt.Sprintf(
|
|
||||||
"Version: %s\nBuild Date: %s\nCommit: %s\nGo Version: %s",
|
|
||||||
Version, BuildTime, Commit, GoVersion,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user