replace broken old implemtation with a new script version

This commit is contained in:
2026-02-13 15:31:19 +01:00
parent 4a3c67e44b
commit c479ceaa5f
5 changed files with 21 additions and 237 deletions

4
.gitignore vendored
View File

@@ -1,4 +0,0 @@
rpm
pkg
src
bin

View File

@@ -5,29 +5,14 @@ __telwho__ is a command line tool for accessing the PSI phonebook.
# Usage
```
Usage: telwho <expr>
```
# Development
The binary can be (cross)compiled as follows:
```
# Linux
GOPATH=`pwd` GOOS=linux GOARCH=amd64 go build telwho.go
# Windows
GOPATH=`pwd` GOOS=windows GOARCH=amd64 go build telwho.go
# Mac OS X
GOPATH=`pwd` GOOS=darwin GOARCH=amd64 go build telwho.go
telwho name_or_phone_or_group
```
# Packaging
The RPM package can be build as follows:
```
git clone https://git.psi.ch/gfa_rpms/gfa-telwho
git clone https://gitea.psi.ch/gfa_rpms/gfa-telwho
cd gfa-telwho
rpmbuild -ba gfa-telwho.spec
rpmbuild -bb gfa-telwho.spec
```

View File

@@ -1,50 +1,27 @@
Summary: The PSI command line phonebook
Name: gfa-telwho
Obsoletes: telwho
Version: 2.3
Release: 0%{?dist}
Version: 3
Release: 0
License: GPL
Group: Applications/System
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n)
Vendor: PSI GFA Controls
URL: git@git.psi.ch:gfa_rpms/gfa-telwho.git
BuildRequires: golang
URL: git@gitea.psi.ch:gfa_rpms/gfa-telwho.git
BuildArch: noarch
Requires: bash curl jq sed util-linux
%define prog_folder /usr/local/bin
%define debug_package %{nil}
%define git_folder %{_sourcedir}/%{name}-%{version}
%define _use_internal_dependency_generator 0
%define __find_provides %{nil}
%define __find_requires %{nil}
%define _builddir %(pwd)
%description
The good old command line phonebook
%prep
%{__rm} -rf %{git_folder}
git clone %{url} %{git_folder}
%build
GOPATH=`pwd` GOOS=linux GOARCH=amd64 go build %{git_folder}/telwho.go
%install
pwd
%{__rm} -rf $RPM_BUILD_ROOT
%{__mkdir_p} $RPM_BUILD_ROOT/usr/local/bin
%{__cp} -a %{_builddir}/telwho $RPM_BUILD_ROOT/usr/local/bin/telwho
%clean
%{__rm} -rf $RPM_BUILD_ROOT
%{__rm} -rf %{git_folder}
%post
%preun
%{__cp} -a telwho $RPM_BUILD_ROOT/usr/local/bin/telwho
%files
%defattr(-,root,root,0755)
/usr/local/bin/telwho

10
telwho Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
echo -ne "\U1F4A4\r"; # It may take a while at first access
url=https://intranet.psi.ch/de/search/profiles/export
keywords=`echo -n $* | jq -sRr @uri` # uri encode
curl -s $url?keywords=$keywords \
| sed 's/"//g;s/ / /g' \
| column -s, -t -H 2,3,4,5

184
telwho.go
View File

@@ -1,184 +0,0 @@
package main
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"regexp"
"strings"
"time"
)
func main() {
longPhone := flag.Bool("i", false, "show international phone number instead of internal")
email := flag.Bool("e", false, "show email address")
unixName := flag.Bool("u", false, "show unix name")
flag.Parse()
var expression string
arguments := flag.Args()
if len(arguments) >= 1 {
expression = arguments[0]
} else {
fmt.Println("Usage: telwho <expr>")
os.Exit(-1)
}
// flag.Usage = func() {
// fmt.Printf("Usage of %s:\n", os.Args[0])
// fmt.Printf(" pattern ...\n")
// flag.PrintDefaults()
// }
//
// flag.Parse()
// Write cache file
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
cachefileName := usr.HomeDir + "/.telwho"
var bodyBytes []byte
// expression := flag.Args()[1]
c := &http.Client{
Timeout: 1 * time.Second,
}
response, err := c.Get("https://secret-downloads.intranet.psi.ch/telwho/telwho.csv")
if err != nil {
// log.Warning("Unable to contact server")
log.Print("Unable to reach server - read cache file " + cachefileName)
// Read data from cache file - if exists
if _, err := os.Stat(cachefileName); err == nil {
// path/to/whatever exists
bodyBytes, err = ioutil.ReadFile(cachefileName)
if err != nil {
log.Fatal("Unable to read cache file")
return
}
} else {
log.Fatal("Cache file does not exist")
return
}
// log.Fatal(err)
} else {
defer response.Body.Close()
bodyBytes, _ = ioutil.ReadAll(response.Body)
cachefile, err := os.Create(cachefileName)
if err != nil {
log.Fatal(err)
}
defer cachefile.Close()
cachefile.Write(bodyBytes)
// io.Copy(cachefile, bodyBytes)
// ignore errors
}
rd := csv.NewReader(bytes.NewReader(bodyBytes))
// r := csv.NewReader(response.Body)
rd.Comma = '\t'
// Ignore case
regularExpression := regexp.MustCompile("(?i)" + expression)
headings := []string{"surname", "given name", "title", "id", "gp", "office", "phone"}
formatString := "%-21s%-21s%-11s%-5s%-5s%-11s"
characterCount := 74
if *longPhone {
formatString += "%-17s"
characterCount += 17
} else {
formatString += "%-6s"
characterCount += 6
}
if *unixName {
headings = append(headings, "username")
formatString += "%-16s"
characterCount += 16
}
if *email {
headings = append(headings, "email")
formatString += "%-16s"
characterCount += 16
}
// convert to a list of interfaces so that it can be used in printf
headingsList := make([]interface{}, len(headings))
for i := range headings {
headingsList[i] = headings[i]
}
// 21 21 11 5 5 11 5
// fmt.Println("surname given name title id gp office phone")
fmt.Printf(formatString+"\n", headingsList...)
fmt.Println(strings.Repeat("=", characterCount))
for {
record, err := rd.Read()
// _, err := rd.Read()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
// log.Fatal(err)
}
// records := regexp.MustCompile(` +`).Split(record, -1)
fields := []string{record[3], record[4], record[2], record[5], record[8], record[6] + "/" + record[7]}
if *longPhone {
fields = append(fields, strings.Replace(record[9], " ", "", -1))
} else {
shortPhone := strings.Replace(record[9], " ", "", -1)
if len(shortPhone) >= 4 {
shortPhone = shortPhone[len(shortPhone)-4:]
}
fields = append(fields, shortPhone)
}
if *unixName {
fields = append(fields, record[10])
}
if *email {
fields = append(fields, record[11])
}
fieldsList := make([]interface{}, len(fields))
for i := range fields {
fieldsList[i] = fields[i]
}
recordString := fmt.Sprintf(formatString, fieldsList...)
if regularExpression.MatchString(recordString) {
fmt.Println(recordString)
}
}
// _, err = io.Copy(os.Stdout, response.Body)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(expression)
}