Skip to content

Commit

Permalink
shorthen devel build version string
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Sep 5, 2017
1 parent f0a29dc commit a1c72ee
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"fmt"
"os"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -69,11 +70,13 @@ func processes() {
ps := goprocess.FindAll()

var maxPID, maxPPID, maxExec, maxVersion int
for _, p := range ps {
for i, p := range ps {
ps[i].BuildVersion = shortenVersion(p.BuildVersion)
maxPID = max(maxPID, len(strconv.Itoa(p.PID)))
maxPPID = max(maxPPID, len(strconv.Itoa(p.PPID)))
maxExec = max(maxExec, len(p.Exec))
maxVersion = max(maxVersion, len(p.BuildVersion))
maxVersion = max(maxVersion, len(ps[i].BuildVersion))

}

for _, p := range ps {
Expand All @@ -99,6 +102,19 @@ func processes() {
}
}

var develRe = regexp.MustCompile(`devel\s+\+\w+`)

func shortenVersion(v string) string {
if !strings.HasPrefix(v, "devel") {
return v
}
results := develRe.FindAllString(v, 1)
if len(results) == 0 {
return v
}
return results[0]
}

func usage(msg string) {
if msg != "" {
fmt.Printf("gops: %v\n", msg)
Expand Down
41 changes: 41 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import "testing"

func Test_shortenVersion(t *testing.T) {
type args struct {
v string
}
tests := []struct {
version string
want string
}{
{
version: "go1.8.1.typealias",
want: "go1.8.1.typealias",
},
{
version: "go1.9",
want: "go1.9",
},
{
version: "go1.9rc",
want: "go1.9rc",
},
{
version: "devel +990dac2723 Fri Jun 30 18:24:58 2017 +0000",
want: "devel +990dac2723",
},
}
for _, tt := range tests {
t.Run(tt.version, func(t *testing.T) {
if got := shortenVersion(tt.version); got != tt.want {
t.Errorf("shortenVersion() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit a1c72ee

Please sign in to comment.