Skip to content

Instantly share code, notes, and snippets.

@arpanetus
Created August 15, 2022 01:17
Show Gist options
  • Save arpanetus/15a681250bae39d103d6ade6ab4c56ae to your computer and use it in GitHub Desktop.
Save arpanetus/15a681250bae39d103d6ade6ab4c56ae to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"os/exec"
"time"
)
type App struct {
Path string
Name string
Launched time.Time
DailyLimit time.Duration
LeftLimit time.Duration
}
type AppMap map[string]App
const timeDetailsPath = "/tmp/app-details"
func fillCmapIfNot() AppMap {
return AppMap{
"/bin/app": {
Name: "app",
Path: "/bin/app",
DailyLimit: 30 * time.Second,
LeftLimit: 30 * time.Second,
},
}
}
func main() {
cmap := fillCmapIfNot()
var app App
for _, v := range cmap {
app = v
}
ctx, _ := context.WithTimeout(context.Background(), app.LeftLimit)
cmd := exec.CommandContext(ctx, app.Path)
if err := cmd.Start(); err!=nil {
fmt.Printf("err: %+v\n", err)
}
appStart := time.Now()
if err := cmd.Wait(); err!=nil {
fmt.Printf("err: %+v\n", err)
}
appFinish := time.Now()
usageDuration := appFinish.Sub(appStart)
app.LeftLimit = app.LeftLimit - usageDuration
fmt.Printf(
"you've been using %s for %s out of %s, left %s",
app.Name,
usageDuration,
app.DailyLimit,
app.LeftLimit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment